简体   繁体   English

MySQL从3个表中选择多个条件

[英]MySQL Select from 3 tables where multiple conditions

I have a problem selecting data from 3 MySQL tables. 我从3个MySQL表中选择数据时遇到问题。

I want to show the following columns: 我想显示以下列:

SELECT table1.id as UserID, table1.gender, table1.land, table1.dob, table1.category_id, table1.is_participant, table2.*

The condition I want to use is: 我想要使​​用的条件是:

WHERE table1.id = table2.user_id
OR table3.id = table2.responder_id

So the entire query I am trying to use is: 所以我想要使用的整个查询是:

SELECT table1.id as UserID, table1.gender, table1.land, table1.dob, table1.category_id, table1.is_participant, table2.*
FROM table1, table2, table3
WHERE table1.id = table2.user_id
OR table3.id = table2.responder_id

What is wrong with this query? 这个查询有什么问题? For some reason, every time I ask this query, the process never ends. 出于某种原因,每次我询问此查询时,该过程永远不会结束。

Thanks for your help! 谢谢你的帮助!

Never ends means that the query synthax is right and correctly parsed by the engine, but the execution is slow . Never ends意味着查询synthax是正确的并且由引擎正确解析,但执行速度很慢。 How much data do you have in that table ? 你在那张桌子里有多少数据?

Take a look at indexes on that table and on the data structure optimization. 看一下该表上的索引和数据结构优化。 However try using a JOIN in this way : 但是请尝试以这种方式使用JOIN:

SELECT table1.id as UserID, table1.gender, table1.land, table1.dob, table1.category_id, table1.is_participant, table2.*
FROM table1
JOIN table2 ON table1.id = table2.user_id 
JOIN table3 ON table3.id = table2.responder_id

I suggest using joins explicitly: 我建议明确使用连接:

FROM table2 
    LEFT JOIN table1 ON table1.id = table2.user_id
    LEFT JOIN table3 ON table3.id = table2.responder_id
WHERE table1.id IS NOT NULL OR table3.id IS NOT NULL;

Since you have OR operand you need to use LEFT JOIN here: this way you'll have records from all the tables, and then perform WHERE ... OR on them. 由于你有OR操作数,你需要在这里使用LEFT JOIN :这样你将拥有所有表中的记录,然后对它们执行WHERE ... OR

Tutorial on joins here 导师制加入这里

use this code: 使用此代码:

SELECT 
table1.id as UserID, 
table1.gender, 
table1.land, 
table1.dob, 
table1.category_id, 
table1.is_participant, 
table2.*,
table3.* // you not mention the table3 field, i just mention the all fields 
FROM table1
join table2 
on table1.id = table2.user_id
join table3
on table3.id = table2.responder_id

Note: I had an one doubt, your using the the table3 but not mention the field name . 注意: 我有一个疑问,你使用table3但没有提到field name

Your question is vague, first tell what you are trying to achieve. 你的问题很模糊,先说出你想要达到的目的。 I think your query is wrong, by the way its taking alot of time because of OR condition that you have added. 我认为你的查询是错误的,因为你已经添加了很多时间,因为你已经添加了OR条件。

When you write FROM table1, table2, table3 it means MySql has to create a temporary table which will contain all permutations of all the rows in all three tables. 当您编写FROM table1,table2,table3时,这意味着MySql必须创建一个临时表,该表将包含所有三个表中所有行的所有排列。 Thats why you limit number of permutations that MySql has to make by adding proper WHERE condition. 这就是为什么你通过添加适当的WHERE条件来限制MySql必须进行排列的数量。

I think you need to add AND condition instead of OR. 我认为你需要添加AND条件而不是OR。

Also make sure table1.id, table2.user_id, table3.id and table2.responder_id are indexed if you want faster result. 如果想要更快的结果,还要确保将table1.id,table2.user_id,table3.id和table2.responder_id编入索引。

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

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