加入3个表后,MySQL返回空结果

时间:2018-07-20 08:27:58

标签: mysql sql

我将四张桌子连在一起。它工作成功。今天起,它不起作用。执行查询后没有错误,但MySQL返回空结果集,我不为什么。我在下面编写了表和查询

类别

Id cat_name status
1   tv         1
2   mobile     1

品牌

Id brand_name status
1   samsung     1
2    nokia      1

折扣

id  barcode_id  discount_name  discount_type    amount
 1    222            fix         percentage       5  

产品表

Id p_name    p_description cat_id brand_id warranty cost_price retail_price 
1  samsung4  samsung4 only  2         1       2        30000     40000

reorderlevel barcode   add_date     status
20              222   2018-07-20      1

sql查询

 select p.id,p.p_name,p.p_description,c.catname,b.brand_name,p.warranty,p.cost_price,p.retail_price,d.discount_type,d.amount,p.reorderlevel,p.barcode,p.add_date,p.status from product p, brand b, category c,discount d where p.brand_id = b.id and p.cat_id = c.id and p.barcode = d.barcode_id and p.barcode = '222'

执行查询后没有错误,但是MySQL返回了空结果集

2 个答案:

答案 0 :(得分:0)

我认为折扣表中没有带有222条码的行 否则,请尝试使用联接而不是逗号分隔的查询

请检查条形码和条形码ID是否具有相同的数据类型

 select p.id,p.p_name,p.p_description,c.catname,b.brand_name,p.warranty,p.cost_price,p.retail_price,d.discount_type,d.amount,p.reorderlevel,p.barcode,p.add_date,p.status 
from product p
join brand b on p.brand_id = b.id
join category c on p.cat_id = c.id
join discount d on p.barcode = d.barcode_id
where p.barcode = '222'

答案 1 :(得分:0)

从语法上讲,您的查询似乎不错。根据您提供的数据,在外观上看起来数据还可以,但是我们看不到一些细节(例如尾随/前导空白或类型不匹配)。

使用“来自[表]的显示列”,确保您的列类型与您尝试进行比较的位置相匹配。 MySQL会强制转换类型,但是您的类型应该对齐。

第二,确保联接中使用的任何ID中都没有前导/尾随空格-我们很难从提供的信息中看到是否确实如此,但是如果您有任何疑问,它将为您解释问题连接中使用的列是char类型。