简体   繁体   English

根据其他表的条件从表中选择数据

[英]Selecting data from a table based on conditions with other tables

I'm really unsure how best to go about writing this query. 我真的不确定如何最好地编写此查询。 I have 3 tables and I need to run a query pulling data from one, based on conditions in the others. 我有3个表,我需要运行一个查询,根据另一个条件从一个表中提取数据。

Tables: surveys, survey_countries, survey_categories 表格:调查,调查国家/地区,调查类别

surveys : 调查
id | id | survey_id | survey_id | network_id network_id

survey_countries : survey_countries
id | id | survey_id | survey_id | network_id | network_id | country 国家

survey_categories : id | survey_categories :id | survey_id | survey_id | network_id | network_id | category 类别

(The survey_id in survey_countries and survey_categories relates to the survey_id column in the surveys table as opposed to the id column). (Survey_countries和survey_categories中的survey_id与Surveys表中的survey_id列相关,而不是与id列相关)。

I need to retrieve data from surveys for a specific country and a specific category. 我需要从针对特定国家和特定类别的调查中检索数据。 Then I need to be able to UNION for other categories, but I guess I can do that later. 然后,我需要能够对其他类别使用UNION,但我想以后可以这样做。 My attempt: 我的尝试:

SELECT surveys.*
FROM survey_countries
LEFT JOIN surveys ON survey_countries.survey_id = surveys.survey_id ANd survey_countries.network_id = surveys.network_id
LEFT JOIN survey_categories ON survey_countries.survey_id = surveys.survey_id AND survey_categories.network_id = surveys.network_id
WHERE survey_countries.country = 'GB'
AND survey_categories.category = 'my_category'
GROUP BY surveys.id

Thanks! 谢谢!

EDIT: the following seems to work: 编辑:以下似乎工作:

SELECT s.*, ca.category, co.country
FROM surveys s
LEFT JOIN survey_countries co ON s.survey_id = co.survey_id AND s.network_id = co.network_id
LEFT JOIN survey_categories ca ON s.survey_id = ca.survey_id AND s.network_id = ca.network_id
WHERE ca.category = 'uncategorised'
AND co.country = 'GB';

I'm just not sure it's the best way to do it since I need to grab surveys with multiple categories later on? 我只是不确定这是否是最好的方法,因为以后我需要进行多个类别的调查?

Without example data and an example output, helping you on this query is very difficult. 没有示例数据和示例输出,帮助您进行此查询非常困难。 However..... your 2nd join relates survey_countries with surveys. 但是.....您的第二次加入将Survey_countries与调查相关联。 I think you mean to join it to the survey_categories. 我认为您的意思是将其加入Survey_categories。 Also, you have a Group By, with no aggregate functions. 另外,您还有一个分组依据,没有任何汇总功能。 Get rid of it until you have too much information an need to summarize columns 摆脱它,直到您需要太多信息来汇总列

I am not saying this is correct, but it is slightly more 'correct' than yours 我并不是说这是正确的,但是它比您的“正确”的稍微多一点

SELECT surveys.*
FROM survey_countries
LEFT JOIN surveys 
  ON survey_countries.survey_id = surveys.survey_id 
    AND survey_countries.network_id = surveys.network_id
LEFT JOIN survey_categories 
  ON survey_categories.survey_id = surveys.survey_id 
    AND survey_categories.network_id = surveys.network_id
WHERE survey_countries.country = 'GB'
  AND survey_categories.category = 'my_category'

(If you can, load up a schema and sample data in sqlfiddle. It will make this problem easier to solve.) (如果可以,请在sqlfiddle中加载架构并采样数据。这将使此问题更易于解决。)

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

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