简体   繁体   English

MySQL UNION SELECT 3表错误

[英]MySQL UNION SELECT 3 Tables Error

i am trying to UNION Select 3 Tables: admin_table, moderator_table, user_table. 我正在尝试UNION选择3个表:admin_table,moderator_table,user_table。

the three share the same columns: uid, username, password. 这三个共享相同的列:uid,用户名和密码。

here is their contents. 这是他们的内容。

admin_table
uid   |   username   |   password
---------------------------------------
1001  |   admin01    |   lakK4wAsln@ZGK


moderator_table
uid   |   username   |   password
---------------------------------------
2001  |   mod01      |   jb#0NAL837AjLj


user_table
uid   |   username   |   password
---------------------------------------
3001  |    user01    |   sdndLaKn$RfGK:

the problem is when i execute this query: 问题是当我执行此查询时:

SELECT * FROM admin_table UNION SELECT * FROM moderator_table UNION SELECT * FROM user_table WHERE username = "admin01";

the result is 结果是

uid   |   username   |   password
---------------------------------------
1001  |   admin01    |   lakK4wAsln@ZGK
2001  |   mod01      |   jb#0NAL837AjLj

as you can see the mod01 is appearing whereas my query was to show entries with username being 'admin01' .. i've tried dropping the table and making a new one but it still does the same output. 如您所见,mod01出现了,而我的查询是显示用户名为“ admin01”的条目。.我尝试删除表并制作一个新表,但它仍执行相同的输出。 i hope someone can help me out. 我希望有人能帮助我。 thanks 谢谢

The WHERE clause is only applying to the last UNION. WHERE子句仅适用于最后一个UNION。

How about this: 这个怎么样:
SELECT * FROM admin_table WHERE username = "admin01" SELECT * FROM admin_table WHERE username =“ admin01”
UNION 联盟
SELECT * FROM moderator_table WHERE username = "admin01" SELECT * FROM moderator_table WHERE用户名=“ admin01”
UNION 联盟
SELECT * FROM user_table WHERE username = "admin01" SELECT * FROM user_table WHERE username =“ admin01”

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

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