我创建了两个名为"posts"的表,其字段分别为id、post_name和'post_meta,其字段为id、post_id、meta_name、meta_vale。。
然后我插入了一些这样的值。
在后表中:-
+----+--------------+
| id | post_name |
+----+--------------+
| 15 | fsdfsd |
| 16 | fsdgbfg fsd |
+----+--------------+
在Post_meta表中:-
+----+---------+-----------+------------+
| id | post_id | meta_name | meta_value |
+----+---------+-----------+------------+
| 1 | 15 | image | image1.jpg |
| 2 | 15 | image | image2.jpg |
| 3 | 16 | image | image3.jpg |
| 4 | 15 | image | image4.jpg |
+----+---------+-----------+------------+
我想创建一个查询来获得类似的输出
+----+-------------+-------------+
| id | post_name | meta_value |
+----+-------------+-------------+
| 15 | fsdfsd | image1.jpg |
| 16 | fsdgbfg fsd | image3.jpg |
+----+-------------+-------------+
只有一个元值和它的post_id。。。
请帮帮我。
select m.id, p.post_name, m.meta_value
from posts p
inner join post_meta m on p.id = m.post_id
inner join
(
select post_id, min(id) as minid
from post_meta
group by post_id
) m2 on m.post_id = m2.post_id and m.id = m2.minid
SELECT a.id,a.post_name,b.meta_value
FROM posts as a
INNER JOIN post_meta a p ON a.id=b.post_id
Select * from Post_meta, Post where
(conditions)
Join Post_meta on Post_meta.Post_id = Post.id
这应该做到:
SELECT pm.id, p.post_name, pm.meta_value
FROM posts p
INNER JOIN post_meta pm ON pm.post_id = p.id
ORDER BY pm.id ASC
GROUP BY pm.post_id