简体   繁体   English

MySQL右连接查询不返回任何值

[英]mysql right join query returning no values

I am trying to return the top 10 'brands' which share a 'transaction id' with the selected brand ($brand). 我试图返回与所选品牌($ brand)共享“交易ID”的前10个“品牌”。 I began with a basic query which queries 1 table with the columns id and brand. 我从一个基本查询开始,该查询用ID和Brand列查询1个表。

I am now wanting to get this to work on 2 separate tables(one for transactions and products respectively), with a common field (sku). 我现在想让它在2个单独的表(分别用于交易和产品)上工作,并具有一个公共字段(sku)。

I began with this query, to get the initial logic (credit to a stackoverflow contributor), and it works perfectly. 我从此查询开始,以获取初始逻辑(贷给stackoverflow贡献者),并且它运行完美。

$brand = $_POST['brand'];
$query = $db->prepare("SELECT brand
FROM transactions
WHERE
id IN (SELECT id FROM transactions WHERE brand = :brand)
AND brand <> :brand
GROUP BY brand
ORDER BY COUNT(*) DESC
LIMIT 10");
$query->bindparam(":brand", $brand);
$query->execute();

The above works great as I mentioned. 就像我提到的,上面的作品很棒。 And I modified it slightly below to incorporate related tables. 我在下面做了一些修改,以合并相关表格。 BUT, it doesnt work. 但是,它不起作用。 and returns no values. 并且不返回任何值。

Any takers. 任何人。 Much appreciated - Adam 非常感谢-亚当

$brand = $_POST['brand'];
$query = $db->prepare("SELECT `2_products`.`brand` 
FROM `2_products` 
RIGHT JOIN `1_txns` 
ON `2_products`.`sku`=`1_txns`.`sku` 
WHERE 
`1_txns`.`txn_id` IN (SELECT `1_txns`.`txn_id` FROM `1_txns` WHERE `2_products`.`brand` = :brand) 
AND `2_products`.`brand` <> :brand 
GROUP BY `2_products`.`brand` 
ORDER BY COUNT(*) DESC 
LIMIT 10");
$query->bindparam(":brand", $brand);
$query->execute();

I understand that there may be other methods to this such as create temp table, with just txn_id and brand columns, but again I'm unsure of how to do this. 我知道可能还有其他方法,例如创建临时表,仅txn_idbrand列,但是我不确定如何执行此操作。

edit attempt #2 - Here as per advice below I have moved the AND 2_products . 编辑尝试#2-根据下面的建议,我已经移动了AND 2_products brand <> :brand to immediately after the ON clause. brand <>:品牌到ON子句之后。 Still 0 rows returned. 仍然返回0行。

    $brand = $_POST['brand'];
$query = $db->prepare("SELECT `2_products`.`brand` 
FROM `2_products` 
RIGHT JOIN `1_txns` 
ON `2_products`.`sku`=`1_txns`.`sku` 
AND `2_products`.`brand` <> :brand
WHERE `1_txns`.`txn_id` IN (SELECT `1_txns`.`txn_id` FROM `1_txns` WHERE `2_products`.`brand` = :brand) 
GROUP BY `2_products`.`brand` 
ORDER BY COUNT(*) DESC 
LIMIT 10");
$query->bindparam(":brand", $brand);
$query->execute();

EDIT 3: Also taking into consideration the fact that in my inner select query the where clause is a column which is not selected from the inner query. 编辑3:还考虑到在我的内部选择查询中where子句是未从内部查询中选择的列的事实。 and so I tried this: To which I got an error 'Operand should only contain 1 column(s)' 所以我尝试了这个:我收到一个错误信息:“操作数应只包含1列”

    SELECT `2_products`.`brand` 
FROM `2_products` 
RIGHT JOIN `1_txns` 
ON `2_products`.`sku`=`1_txns`.`sku` 
AND `2_products`.`brand` <> :brand 
WHERE `1_txns`.`txn_id` IN (SELECT `1_txns`.`txn_id`, `2_products`.`brand` 
                            FROM `1_txns` 
                            LEFT JOIN `2_products` 
                            ON `1_txns`.`sku`=`2_products`.`sku` 
                            WHERE `2_products`.`brand` = :brand) 
GROUP BY `2_products`.`brand` 
ORDER BY COUNT(*) DESC 
LIMIT 10;

When a LEFT / RIGHT join return no records, first check if WHERE has any filters referring to the table on the "weak" side. LEFT / RIGHT不返回任何记录时,请首先检查WHERE是否有任何过滤器引用“弱”侧的表。 By "weak" side I mean the right table on a LEFT JOIN , or the left table on a RIGHT JOIN . 所谓“弱”,是指LEFT JOIN上的RIGHT JOIN表或RIGHT JOIN上的左表。

In your case, the left table is 2_products , so it might generate NULL fields, but in WHERE you have 2_products.brand <> :brand . 在您的情况下,左表是2_products ,因此它可能会生成NULL字段,但是在WHERE您具有2_products.brand <> :brand If this condition fails, the record will be removed. 如果此条件失败,则记录将被删除。

To avoid this, try moving this condition to the ON clause, after 2_products.sku=1_txns.sku , because filtering there will not remove the record if the condition fails. 为避免这种情况,请尝试将条件移到2_products.sku=1_txns.sku之后的ON子句中,因为如果条件失败,则在2_products.sku=1_txns.sku过滤不会删除记录。 It should look this way: 它应该看起来像这样:

ON 2_products.sku=1_txns.sku AND 2_products.brand <> :brand

And the WHERE clause should look like this: WHERE子句应如下所示:

WHERE 1_txns.txn_id IN (SELECT 1_txns.txn_id FROM 1_txns WHERE 2_products.brand = :brand)

This was solved in the following question on dba.stackexchange 在dba.stackexchange上的以下问题中解决了此问题

DBA Question DBA问题

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

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