after join 3表mysql返回空结果

cuxqih21  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(296)

我把四张table连在一起,结果很成功。从今天起它就不起作用了。在执行查询之后,没有错误,但是mysql返回了一个空的结果集,我不知道为什么
类别

  1. Id cat_name status
  2. 1 tv 1
  3. 2 mobile 1

品牌

  1. Id brand_name status
  2. 1 samsung 1
  3. 2 nokia 1

折扣

  1. id barcode_id discount_name discount_type amount
  2. 1 222 fix percentage 5

产品表

  1. Id p_name p_description cat_id brand_id warranty cost_price retail_price
  2. 1 samsung4 samsung4 only 2 1 2 30000 40000
  3. reorderlevel barcode add_date status
  4. 20 222 2018-07-20 1

SQL查询

  1. 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返回了一个空的结果集

6za6bjd0

6za6bjd01#

从语法上看,您的查询似乎很好。根据您提供的数据,从视觉上看,数据看起来也不错,但我们看不到一些细节(例如尾随/前导空格或不匹配的类型)。
使用“show columns from[table]”,确保列类型与要进行比较的位置匹配。mysql确实强制类型转换,但是您的类型应该对齐。
第二,确保在连接中使用的任何ID中都没有前导/尾随空格-我们很难从提供的信息中看出是否确实如此,但如果连接中使用的任何列都是char类型,这将解释您的问题。

zrfyljdw

zrfyljdw2#

我想折扣表中没有带的行 222 请尝试使用连接而不是逗号分隔的查询
请检查条形码和条形码标识是否具有相同的数据类型

  1. 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
  2. from product p
  3. join brand b on p.brand_id = b.id
  4. join category c on p.cat_id = c.id
  5. join discount d on p.barcode = d.barcode_id
  6. where p.barcode = '222'

相关问题