繁体   English   中英

MySQL选择查询,使用某种逻辑从多个表中获取数据

[英]mySQL select query, get data from multiple tables with some logic

我有两个表用户和* activation_details *

用户表具有这些数据。 uid是主键。

uid     |    user_name
______________________
 1     |    John Smith
 2     |    Mary Smith
 3     |    Nancy Smith
 4     |    Agent Smith

activation_details有这些数据

aid     |    is_activated   |  user_id
______________________________________
1      |         0         |     0
2      |         1         |     4
3      |         1         |     1
4      |         1         |     777 

请注意,用户表777中没有用户ID。

我需要这种格式的结果

aid     |    is_activated   |  user_name      
______________________________________________
1       |         0         |     0
2       |         1         |     Agent Smith
3       |         1         |     John Smith
4       |         1         |     777 

如果用户表中存在用户ID,则必须显示用户名,否则必须显示user_id本身。

我尝试过这样的事情

SELECT aid, is_activated, user_id, users.user_name from activation_details, users WHERE users.uid = user_id

它给出了这个输出,没有用。

aid     |    is_activated   |  user_name      | user_id
________________________________________________________ 
2       |         1         |     Agent Smith |     4 
3       |         1         |     John Smith  |     1

无论如何,是否只有使用mysql才能获得此输出,我可以使用PHP中的多个查询来做到这一点,但这不是我想要的。

aid     |    is_activated   |  user_name
______________________________________
1       |         0         |     0
2       |         1         |     Agent Smith
3       |         1         |     John Smith
4       |         1         |     777 

您想结合使用外部联接和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

编辑:我为coalesce()函数又添加了一项检查,如果user_nameuser_id字段均为空值,它将为User返回0

在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;

我认为这个查询会给结果

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM