我创建了两个名为“帖子”的表格,其字段为 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 |
+----+-------------+-------------+
只有一个meta vale和它的post_id ......
请帮帮我。
答案 0 :(得分:1)
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
答案 1 :(得分:0)
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
答案 2 :(得分:0)
Select * from Post_meta, Post where
(conditions)
Join Post_meta on Post_meta.Post_id = Post.id
答案 3 :(得分:0)
这应该这样做:
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