简体   繁体   English

MySQL SELECT全部来自第一个表,并使用第二个表过滤器的特定where子句连接来自第二个表的匹配数据

[英]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 这是我的桌子设计

table1: 表格1:

user_id    username
1          test
2          test2
3          test3

table2: 表2:

id         table2_userid   key        value
1          2               position   admin
2          2               name       myname

What i want to output is: 我要输出的是:

user_id     username       key        value
1           test           NULL       NULL
2           test2          position   admin
3           test3          NULL       NULL

This is my current sql code: 这是我当前的sql代码:

 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

Move your condition from where clause to on clause 将条件从where子句移到on子句

SELECT 
t1.user_id, 
t1.username, 
t2.key, 
t2.value 
FROM table1 t1 
LEFT JOIN table2 t2
ON t1.user_id = t2.table2_userid 
AND t2.key="position"

Demo 演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LEFT OUTER JOIN保留第一个表使用的所有匹配结果,而第二个表仅使用 - LEFT OUTER JOIN keep all matched results from first table use where just for second table 从第一个表中选择所有内容并从第二个表中按 Count(id) 排序,其中 second.value = 1 - Select all from first table and order by Count(id) from second table where second.value = 1 SQL-从链接表中选择first_table_id,其中第二个表中的所有记录均为指定类型 - SQL - select first_table_id from linking table where all records in second table are in specified type 如果所有线索均处于活动状态且引线均为0且primary_email =第二表中的是,则MySQL INNER JOIN仅从第一表中选择一行 - MySQL INNER JOIN select only one row from first table if all leads active = 0 and primary_email = Yes in second table 更好的方法来选择第一个表中的所有列,并在内连接上从第二个表中选择一列 - Better way to select all columns from first table and only one column from second table on inner join 根据MySQL中第一个表的结果选择第二个表中的项目 - Select items in second table based on result from first table in MySQL MySQL - 从第二个表中选择与第一个表匹配的最后一个记录 - MySQL - Select last record from second table matching with first table 从第一个表中选择所有结果而不与第二个表匹配 - Select all result from first table without matching with second table 使用第一个表的输出从第二个表中选择特定数据 - Select specific data from second table using output from first table MySQL用第一张表中的数据填充第二张表 - MySQL fill out the second table with data from the first table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM