[英]mySQL select query, get data from multiple tables with some logic
I have two tables users and *activation_details* 我有两个表用户和* activation_details *
Users table has these data. 用户表具有这些数据。 uid is primary key. uid是主键。
uid | user_name
______________________
1 | John Smith
2 | Mary Smith
3 | Nancy Smith
4 | Agent Smith
activation_details has these data activation_details有这些数据
aid | is_activated | user_id
______________________________________
1 | 0 | 0
2 | 1 | 4
3 | 1 | 1
4 | 1 | 777
Please note that user id 777 is not in the users table. 请注意,用户表777中没有用户ID。
I need the result in this format 我需要这种格式的结果
aid | is_activated | user_name
______________________________________________
1 | 0 | 0
2 | 1 | Agent Smith
3 | 1 | John Smith
4 | 1 | 777
If user id is present in the users table then username has to be displayed, else the user_id itself has to be displayed. 如果用户表中存在用户ID,则必须显示用户名,否则必须显示user_id本身。
I tried some thing like this 我尝试过这样的事情
SELECT aid, is_activated, user_id, users.user_name from activation_details, users WHERE users.uid = user_id
It gives this output, which is not useful. 它给出了这个输出,没有用。
aid | is_activated | user_name | user_id
________________________________________________________
2 | 1 | Agent Smith | 4
3 | 1 | John Smith | 1
Is there anyway to get this output using mysql only, I can do it with multiple queries in PHP, but that is not what I am looking. 无论如何,是否只有使用mysql才能获得此输出,我可以使用PHP中的多个查询来做到这一点,但这不是我想要的。
aid | is_activated | user_name
______________________________________
1 | 0 | 0
2 | 1 | Agent Smith
3 | 1 | John Smith
4 | 1 | 777
You want to use an outer join in combination with the coalesce()
function (which returns the first non-null value in the selected columns) like this: 您想结合使用外部联接和coalesce()
函数(该函数返回所选列中的第一个非空值),如下所示:
SELECT
a.aid,
a.is_activated,
a.user_id,
coalesce(b.user_name, a.user_id, 0) as User
from
activation_details a
left outer join users b
on a.user_id=b.uid
Edit: I added one more check for the coalesce()
function which will return a 0
for the User in case both the user_name
and user_id
fields are null values. 编辑:我为coalesce()
函数又添加了一项检查,如果user_name
和user_id
字段均为空值,它将为User返回0
。
Testing done in mysql: 在mysql中完成测试:
mysql> select * from first
-> ;
+------+-------+
| id | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
+------+-------+
3 rows in set (0.00 sec)
mysql> select coalesce('a', id) from first
-> ;
+-------------------+
| coalesce('a', id) |
+-------------------+
| a |
| a |
| a |
+-------------------+
3 rows in set (0.00 sec)
mysql> select coalesce('a', 1) from first;
+------------------+
| coalesce('a', 1) |
+------------------+
| a |
| a |
| a |
+------------------+
3 rows in set (0.00 sec)
mysql> select coalesce(null, 1) from first;
+-------------------+
| coalesce(null, 1) |
+-------------------+
| 1 |
| 1 |
| 1 |
+-------------------+
3 rows in set (0.00 sec)
SELECT t3.aid,t3.is_activated,
IF (t2.user_name IS NOT null, t2.user_name, t3.user_id)
FROM activation_details AS t3
LEFT JOIN users AS t2 ON t3.user_id = t2.uid;
i think this query will give the result 我认为这个查询会给结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.