I have two tables:
1. posts (id, title, content, etc)
2. post_meta (id, post_id, meta_key, meta_value)
I want to get with only one query, all the entries from posts and the entries from post_meta only with post_meta='x'. Some of the entries from posts doesn't have correspondent in post_meta, but I need them. I can do this with only one query?
Select a.*, b.*
From post a
left join post_meta b on (a.id = b.id and b.meta_key='x')
select *
from post
left join
post_meta
where isNull(post_meta.meta_value)
or
post_meta.meta_value ="x"
I haven't done much sql for a while, but I know it is possible. I think it was querying isNull on any value of the table that you are left joining.
select *
from posts p
left outer join post_meta pm on p.id=pm.post_id and pm.meta_key='x'
试试这个..从post.id = post_meta.post_id上的LEFT JOIN post_meta中选择*,其中post_meta.x ='x'
这是我的解决方案。
SELECT * FROM post,post_meta WHERE post.id=post_meta.post_id , post_meta.id="x";
SELECT posts.*, post_meta.*
FROM posts
LEFT JOIN post_meta
ON posts.id = post_meta.post_id WHERE post_meta.meta_value is null OR
post_meta.meta_value = 'x'
Since you want all the posts from posts table whose corresponding meta_value in post_meta table is either 'x' or meta_value doesn't exists, this should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.