简体   繁体   English

在mysql中使用右表的条件左连接

[英]left join with condition for right table in mysql

I have been trying to wrap my head around this issue but I am not making much progress. 我一直试图绕过这个问题,但我没有取得多大进展。 My goal is to do a left join between two tables with criteria for the right table. 我的目标是在两个表之间进行左连接,并使用右表的标准。 I would like to see the list of all products and prices for the current day even though there is no pricing row for the current day. 我想查看当天所有产品和价格的清单,即使当天没有定价行。 Here is an example of the code: 以下是代码示例:

SELECT products.id, products.name, prices.price
FROM products LEFT JOIN prices
ON products.id = prices.id
WHERE prices.date = CURRENT_DATE

This produces only the products with price information for the current date. 这仅生成具有当前日期的价格信息的产品。

OK, so that is just the first part of my issue. 好的,这只是我问题的第一部分。 I would eventually like to pull the price for CURRENT_DATE + INTERVAL 1 DAY as well. 我最终还想把CURRENT_DATE + INTERVAL 1天的价格拉下来。

Any information would be greatly appreciated. 任何信息将不胜感激。

Assuming PRICES.date is a DATETIME data type, use: 假设PRICES.date是DATETIME数据类型,请使用:

   SELECT pd.id, 
          pd.name, 
          pr.price
     FROM PRODUCTS pd
LEFT JOIN PRICES pr ON pr.id = pd.id
                   AND DATE(pr.date) = CURRENT_DATE

I used the DATE function to remove the time portion, because CURRENT_DATE won't include the time portion while DATETIME records will. 我使用DATE函数删除时间部分,因为CURRENT_DATE将不包括DATETIME记录时的时间部分。

In this example, the date criteria is being applied before the JOIN is made. 在此示例中,在JOIN生成之前应用date条件。 It's like a derived table, filtering down the information before the JOIN is made -- which'll produce different results than if the criteria was specified in the WHERE clause. 它就像一个派生表,在JOIN生成之前过滤掉信息 - 这将产生与WHERE子句中指定条件不同的结果。

To get the list of products and prices for tomorrow, use: 要获取明天的产品和价格清单,请使用:

   SELECT pd.id, 
          pd.name, 
          pr.price
     FROM PRODUCTS pd
LEFT JOIN PRICES pr ON pr.id = pd.id
                   AND DATE(pr.date) = DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY)

Reference: 参考:

If you want both todays and tomorrows prices in a single query, use: 如果您想在单个查询中同时获得今天和明天的价格,请使用:

   SELECT pd.id, 
          pd.name, 
          pr1.price AS price_today,
          pr2.price AS price_tomorrow
     FROM PRODUCTS pd
LEFT JOIN PRICES pr1 ON pr1.id = pd.id
                   AND DATE(pr1.date) = CURRENT_DATE
LEFT JOIN PRICES pr2 ON pr2.id = pd.id
                   AND DATE(pr2.date) = DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY)

Strong answers above. 上面的答案很强 Adding to OMG Ponies' reply... having 'right' table criteria in the original WHERE statement basically turns the LEFT OUTER JOIN into an INNER JOIN. 添加到OMG Ponies的回复...在原始WHERE语句中具有'right'表标准基本上将LEFT OUTER JOIN转换为INNER JOIN。 Just a different way of looking at it that might help. 只是以不同的方式看待它可能会有所帮助。

SELECT id,
       name,
       (SELECT price
          FROM prices
         WHERE id = products.id
           AND prices.date = CURRENT_DATE
       ) AS price,
       (SELECT price
          FROM prices
         WHERE id = products.id
           AND prices.date = DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY)
       ) AS price_tomorrow
  FROM products

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

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