简体   繁体   English

MySQL查询,在where子句中使用多个具有相同名称的表字段

[英]MySQL Queries, using multiple table fields with the same name in a where clause

I have a select statement with multiple joins, each one of them has a column name called 'created_on' is there a way to make it so that the where portion of the query will check all 3 tables? 我有一个带有多个联接的select语句,每个联接都有一个名为“ created_on”的列名,有没有一种方法可以使查询的where部分将检查所有3个表? Here is the actual query I made 这是我所做的实际查询

SELECT *
FROM household_address
JOIN catchment_area ON catchment_area.household_id = household_address.household_id
JOIN bhw ON catchment_area.bhw_id = bhw.user_username
JOIN master_list ON master_list.person_id = catchment_area.person_id
JOIN house_visits ON house_visits.household_id = household_address.household_id
WHERE catchment_area.household_id IN (
  SELECT household_address.household_id
  FROM demo.household_address
  JOIN catchment_area ON catchment_area.household_id = household_address.household_id
  JOIN previous_cases ON catchment_area.person_id = previous_cases.person_id
  JOIN active_cases ON catchment_area.person_id = active_cases.person_id
  JOIN ls_report ON ls_report.ls_household = household_address.household_name
  WHERE DATE(created_on) BETWEEN '2014-03-01' AND '2014-03-31' 
)

The joins I am talking about are the joins in the subquery. 我正在谈论的联接是子查询中的联接。

first thing is you should make an alias for each table name like this so that its easier to read and quicker to write.. 首先,您应该像这样为每个表名创建一个别名,以使其更易于阅读和编写。

SELECT *
FROM household_address AS ha
JOIN catchment_area ca ON ca.household_id = ha.household_id
JOIN bhw bh ON ca.bhw_id = bh.user_username
JOIN master_list ml ON ml.person_id = ca.person_id
JOIN house_visits hv ON hv.household_id = ha.household_id
WHERE ca.household_id IN 
(
    SELECT ha1.household_id
    FROM demo.household_address AS ha1
    JOIN ca1 ON ca1.household_id = ha1.household_id
    JOIN previous_cases pc ON ca1.person_id = pc.person_id
    JOIN active_cases ac ON ca1.person_id = ac.person_id
    JOIN ls_report ls_r ON ls_r.ls_household = ha1.household_name
    WHERE DATE(ha1.created_on)  BETWEEN '2014-03-01' AND '2014-03-31' 
      AND DATE(ca1.created_on)  BETWEEN '2014-03-01' AND '2014-03-31'
      AND DATE(pc.created_on)   BETWEEN '2014-03-01' AND '2014-03-31'
      AND DATE(ac.created_on)   BETWEEN '2014-03-01' AND '2014-03-31'
      AND DATE(ls_r.created_on) BETWEEN '2014-03-01' AND '2014-03-31'
)

for each of the where conditional for date created you have to tell it WHERE(table_name.created_on BETWEEN.....) 对于创建日期的每个条件,您必须告诉它WHERE(table_name.created_on BETWEEN .....)

do that for each table and that should work 对每个表都这样做,这应该工作

Try using alias' with additional WHERE clauses. 尝试将别名与其他WHERE子句一起使用。

SELECT 
    *
FROM household_address
JOIN catchment_area ON catchment_area.household_id = household_address.household_id
JOIN bhw ON catchment_area.bhw_id = bhw.user_username
JOIN master_list ON master_list.person_id = catchment_area.person_id
JOIN house_visits ON house_visits.household_id = household_address.household_id
WHERE catchment_area.household_id IN (
  SELECT household_address.household_id
  FROM demo.household_address ha
  JOIN catchment_area ca ON ca.household_id = ha.household_id
  JOIN previous_cases pc ON ca.person_id = pc.person_id
  JOIN active_cases ac ON ca.person_id = ac.person_id
  JOIN ls_report ls_r ON ls_r.ls_household = ha.household_name
  WHERE DATE(ha.created_on) BETWEEN '2014-03-01' AND '2014-03-31' 
  AND DATE(ca.created_on) BETWEEN '2014-03-01' AND '2014-03-31' 
  AND DATE(pc.created_on) BETWEEN '2014-03-01' AND '2014-03-31' 
)

I don't actually know which three tables have the 'created_on' field so I went with household_address, catchment_area and previous_cases as an example. 我实际上不知道哪三个表具有“ created_on”字段,因此我以household_address,catchment_area和previous_cases为例。 Change these to the ones you actually want to search. 将其更改为您实际要搜索的内容。

(also should probably alias all of the table names) (也应该为所有表名加上别名)

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

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