Simple Select INNER JOIN语句 - SQL查询错误

时间:2018-01-16 09:39:04

标签: mysql

我有一个简单的MySQL语句,一直显示错误。

type是表格ps_product_custom_statistics_weekly中的一栏,我只想从type中选择价值为all的值。

SELECT `cat`.`brand_name`
FROM `ps_product_brand` AS `cat`
INNER JOIN `ps_product_custom_statistics_weekly` AS `cat_p` 
ON `cat_p`.`id_product` = `cat`.`id_product`
WHERE `cat_p`.`type` LIKE `%all%`
AND `cat_p`.`rank` <= 16;

错误消息

  

#1054 - Unknown column '%all%' in 'where clause'

(我不明白为什么它告诉我这个,我使用cat_p.type选择列,LIKE %all%从该列中获取字符串,请帮忙)

表:

ps_product_custom_statistics_weekly

  product_id        type            rank
+-------------+--------------+------------------+
|      1      |    weekly    |       20         |
+-------------+--------------+------------------+
|      2      |      all     |       10         |
+-------------+--------------+------------------+

由于

3 个答案:

答案 0 :(得分:1)

问题是你使用的是反引号而不是单引号。像这样使用它:

WHERE `cat_p`.`type` LIKE '%all%'

答案 1 :(得分:1)

您应该引用like条件,如下所示

SELECT `cat`.`brand_name`
FROM `ps_product_brand` AS `cat`
INNER JOIN `ps_product_custom_statistics_weekly` AS `cat_p` 
ON `cat_p`.`id_product` = `cat`.`id_product`
WHERE `cat_p`.`type` LIKE '%all%'//Change quotes
AND `cat_p`.`rank` <= 16;

反引号`不适用于mysql查询中的字符串操作

答案 2 :(得分:0)

解决方案必须

SELECT `cat`.`brand_name`
FROM `ps_product_brand` AS `cat`
INNER JOIN `ps_product_custom_statistics_weekly` AS `cat_p` 
ON `cat_p`.`id_product` = `cat`.`id_product`
WHERE `cat_p`.`type` LIKE '%all%'
AND `cat_p`.`rank` <= 16;

因为喜欢的价值必须在&#39;&#39;不在“