我有几个要查询的项ID,即使它们是几个。我解释一下:
我有一张桌子id | item
我有一些要查询的ID(例如25,62,89,123,12,25,25,62)
我需要显示所有这些,即使有多次相同的id,也不需要进行多次查询。如果我使用“where in
”查询,它会对ID进行分组,因此只显示一次ID 25,例如......
我怎么能设法做到这一点?
我的查询:
$result = mysqli_query($link,"select p.id_product, p.price, c.link_rewrite as catrew, i.id_image, l.link_rewrite, l.name, s.reduction, s.reduction_type
from rec_product p
left join rec_image i on i.id_product=p.id_product and i.cover=1
left join rec_product_lang l on l.id_product=p.id_product and l.id_lang = 1
left join rec_category_lang c on c.id_category=p.id_category_default
left join rec_specific_price s on s.id_product=p.id_product
WHERE p.id_product IN ($id_prod_array)
order by rand()");
示例:
$id_prod_array = "13,13,119";
结果:
预期:
第一个(id 13)的2倍和id 119的一次
[编辑]
找到解决方案:id为13,13,119的示例
select p.id_product, p.price, c.link_rewrite as catrew, i.id_image, l.link_rewrite, l.name, s.reduction, s.reduction_type
from rec_product p
join (select 13 as id_product
union all select 13
union all select 119) as T1 on p.id_product = T1.id_product
left join rec_image i on i.id_product=p.id_product and i.cover=1
left join rec_product_lang l on l.id_product=p.id_product and l.id_lang = 1
left join rec_category_lang c on c.id_category=p.id_category_default and c.id_lang = 1
left join rec_specific_price s on s.id_product=p.id_product
答案 0 :(得分:1)
您需要FIND_IN_SET
功能才能搜索$id_prod_array
的值。
更改:
WHERE p.id_product IN ($id_prod_array)
致:
WHERE FIND_IN_SET( p.id_product, REPLACE( $id_prod_array, ' ', '' ) ) > 0
它应该有效。
您的查询无效的原因分析如下:
如果
`$id_prod_array` = '25, 62, 89, 123, 12, 25, 25, 62'
然后假设当与sql IN
一起使用时,它等同于:
WHERE p.id_product IN ( 25, 62, 89, 123, 12, 25, 25, 62 )
但是,它相当于:
WHERE p.id_product IN ( '25, 62, 89, 123, 12, 25, 25, 62' )
含义:IN
'25, 62, 89, 123, 12, 25, 25, 62' != 25, 62, 89, 123, 12, 25, 25, 62
示例 :
mysql> select 89 in ( 25, 62, 89, 123, 12, 25, 25, 62 );
+-------------------------------------------+
| 89 in ( 25, 62, 89, 123, 12, 25, 25, 62 ) |
+-------------------------------------------+
| 1 |
+-------------------------------------------+
1 row in set (0.08 sec)
mysql> select 89 in ( '25, 62, 89, 123, 12, 25, 25, 62' );
+---------------------------------------------+
| 89 in ( '25, 62, 89, 123, 12, 25, 25, 62' ) |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
1 row in set, 1 warning (0.04 sec)
mysql> show warnings;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------|
| Warning | 1292 | Truncated incorrect DOUBLE |
| | | value: '25, 62, 89, 123, 12, 25, 25, 62' |
+---------+------+------------------------------------------+
请参阅 :