[英]MySQL SELECT all from first table and join matched data from second table with specific where clause for second table filter
I have an issue when trying to fetch out my data from my database. 尝试从数据库中获取数据时出现问题。
here are my table design 这是我的桌子设计
I have an issue when trying to fetch out my data from my database. 尝试从数据库中获取数据时出现问题。
here are my table design 这是我的桌子设计
user_id username
1 test
2 test2
3 test3
id table2_userid key value
1 2 position admin
2 2 name myname
user_id username key value
1 test NULL NULL
2 test2 position admin
3 test3 NULL NULL
SELECT table1.user_id, table1.username, table2.key, table2.value
FROM table1
LEFT JOIN table2 ON table1.user_id = table2.table2_userid WHERE table2.key="position"
However, this return nothing. 但是,这什么也没有返回。 Please help me in this.
请帮助我。
Thanks. 谢谢。
Try following Query it will work for your problem: 尝试执行以下查询,它可以解决您的问题:
SELECT table1.user_id, table1.username, table2.key, table2.value FROM
table1 LEFT JOIN table2 ON table1.user_id = table2.table2_userid and
table2.key="position" group by table1.user_id
Try using single quotes: 尝试使用单引号:
SELECT table1.user_id, table1.username, table2.key, table2.value
FROM table1
LEFT JOIN table2 ON table1.user_id = table2.table2_userid WHERE table2.key = 'position'
Otherwise your query seems fine to me. 否则,您的查询对我来说似乎不错。
I've read this on SO in a comment: [S]ingle for [S]trings; [D]ouble for [D]atabase
我已经在SO中阅读了以下评论:
[S]ingle for [S]trings; [D]ouble for [D]atabase
[S]ingle for [S]trings; [D]ouble for [D]atabase
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.