简体   繁体   English

选择其他表中不存在值的行

[英]Selecting rows where value don't exist in other table

I've got two tables. 我有两张桌子。 One for main accounts and one for banned accounts and I wanna select all the members in the main accounts and only those who is not banned. 一个用于主要帐户,一个用于禁止帐户,我想选择主要帐户中的所有成员,并且只选择那些未被禁止的成员。

SELECT Username, $field AS Output
FROM `Player_Accounts`, `Ban_List`
WHERE Ban_List.Banned_Name != Username
ORDER BY $field DESC LIMIT 0, 25;

The query take over 1,30min to execute and display the results 该查询花费了1,30分钟的时间来执行和显示结果

IMHO your query is not right. 恕我直言,您的查询不正确。 The number of rows are very high (have you 1.3 million of users???) 行数非常高(拥有130万用户??)

Try this: 尝试这个:

SELECT p.username
FROM Player_Accounts p
WHERE NOT EXISTS(
    SELECT 'BANNED'
    FROM Ban_List b
    WHERE b.banned_name = p.username
)

Alternative: 选择:

SELECT p.username
FROM Player_Accounts p
LEFT OUTER JOIN Ban_List b
ON p.username = b.banned_name
WHERE b.banned_name IS NULL

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

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