gpt4 book ai didi

mysql - 将 INNER JOIN 和 LEFT JOIN 与日期选择结合起来

转载 作者:行者123 更新时间:2023-11-30 00:03:23 24 4
gpt4 key购买 nike

我的数据库有一个复杂的查询,我想在其中连接多个表。
像这样:

SELECT cad.artc, prod.sku, prod.product_id, prodclass.entity_id, prodclass.field_product_class_tid, tax.tid, substring(tax.name, 3), block1.field_product_blocktime_1_value, block1.field_product_blocktime_1_value2
FROM _cadencier as cad
INNER JOIN commerce_product as prod
on prod.sku = cad.artc
AND cad.adrc = '000'
AND cad.relc = '000028'
INNER JOIN field_data_field_product_class as prodclass
on prodclass.entity_id = prod.product_id
INNER JOIN field_data_field_product_limited as limited
on limited.entity_id = prod.product_id
AND limited.field_product_limited_value = '0'
INNER JOIN taxonomy_term_data as tax
ON prodclass.field_product_class_tid = tax.tid
AND tax.language = 'nl'
LEFT JOIN field_data_field_product_blocktime_1 as block1
ON block1.entity_id = prod.product_id
GROUP BY tax.name, prod.title;

给我以下结果:

enter image description here

我想排除今天日期为 NULL 或不在 block 时间值之间的所有产品。但为了做到这一点,我必须使用 LEFT JOIN 而不是 INNER JOIN。但是当我尝试类似的事情时

WHERE block1.field_product_blocktime_1_value >= DATE(NOW())
AND block1.field_product_blocktime_1_value2 <= DATE(NOW())

我最终没有结果......
难道是我加入方式不对?

最佳答案

旧答案:对问题的误解

要也获取左连接表的 NULL 值,您必须将条件包含在连接的 ON 子句中。使用 WHERE 子句中的条件将 OUTER JOIN 隐式更改为 INNER JOIN:

...
LEFT JOIN field_data_field_product_blocktime_1 as block1
ON block1.entity_id = prod.product_id
AND block1.field_product_blocktime_1_value >= DATE(NOW())
AND block1.field_product_blocktime_1_value2 <= DATE(NOW())
GROUP BY tax.name, prod.title;

已编辑答案

如果您只想要那些具有阻止日期的行,那么您必须使用 INNER JOIN。您的 field_product_blocktime_1_value 似乎是区 block 时间的开始,而 block1.field_product_blocktime_1_value2 似乎是区 block 时间的结束。

因为您需要当前日期介于

之间的行
`field_product_blocktime_1_value`

block1.field_product_blocktime_1_value2

您应该交换运算符以获得所需的结果:

...
INNER JOIN field_data_field_product_blocktime_1 as block1
ON block1.entity_id = prod.product_id
WHERE
block1.field_product_blocktime_1_value <= DATE(NOW())
AND
block1.field_product_blocktime_1_value2 >= DATE(NOW())
GROUP BY tax.name, prod.title;

关于mysql - 将 INNER JOIN 和 LEFT JOIN 与日期选择结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24827193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com