加入3个表,其中2个是SELECT *

时间:2012-05-27 03:02:28

标签: mysql join

我正在进行重新测试的研究。我有三张桌子。一张表用于受众特征,我只需要有限的数据。其他两个表各有235个字段,唯一的区别是表名。我尝试过这样的陈述:

SELECT * FROM research_statement_answers, statement_answers, demographic_answers
WHERE (( research_statement_answers.id_Questions = demographic_answers.id_Demographic)
and (statement_answers.id_Questions = demographic_answers.id_Demographic)
and (demographic_answers.Research_Completed = 1))

但这不起作用。我见过的所有例子都要求我在两个表中命名所有235个字段。我能做到这一点,但我无法相信这是最好的方式。因此,建议最受欢迎。谢谢。瑞克

3 个答案:

答案 0 :(得分:1)

SELECT
    research_statement_answers.*,
    statement_answers.*,
    demographic_answers.*
FROM ...

由于这些表仅在名称上有所不同,因此对它们使用UNION JOIN不是更好吗?

结果是你有所有表记录的重聚。

使用普通连接,结果是每条记录235 * 3列,表示三个表之间交集的记录子集。

答案 1 :(得分:0)

您可以通过执行以下操作从多个表中选择*并仅包含第三个表的特定列:

SELECT
    research_statement_answers.*,
    statement_answers.*,
    demographic_answers.id,
    demographic_answers.title,
    demographic_answers.etc
FROM ...

答案 2 :(得分:0)

    SELECT 
    research_statement_answers.*, 
    statement_answers.*,
    demographic_answers.*
 FROM research_statement_answers
    JOIN demographic_answers ON demographic_answers.id_Demographic = research_statement_answers.id_Questions
    JOIN statement_answers ON demographic_answers.id_Demographic = statement_answers.id_Questions
    WHERE demographic_answers.Research_Completed = 1