Mysql:“;“在哪里?”;但我们也需要复制品
我有几个要查询的项目id,即使它们是多个。我解释: 我有一个表Mysql:“;“在哪里?”;但我们也需要复制品,mysql,Mysql,我有几个要查询的项目id,即使它们是多个。我解释: 我有一个表id |项 我有一些ID要查询(例如25、62、89、123、12、25、25、62) 我需要显示所有的id,即使同一个id有好几次,也不需要进行多次查询。如果我使用“where in”查询,它会对id进行分组,因此仅显示一次id 25,例如 请问我怎样才能做到这一点 我的问题是: $result = mysqli_query($link,"select p.id_product, p.price, c.link_rewrite as
id |项
我有一些ID要查询(例如25、62、89、123、12、25、25、62)
我需要显示所有的id,即使同一个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_prod_array = "13,13,119";
预期的:第一个(id 13)的2倍和id 119的1倍 [编辑] 找到解决方案:id为13,13119的示例
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
您需要使用
FIND\u IN\u SET
函数来搜索$id\u prod\u数组的值
更改:
WHERE p.id_product IN ($id_prod_array)
致:
它应该是有效的
您的查询不起作用的原因分析如下:
如果
然后,不要假设在
中与sql一起使用时,它等同于:
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' )
但是,这相当于:
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' )
含义:在
'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' |
+---------+------+------------------------------------------+
请参阅:
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' |
+---------+------+------------------------------------------+
13
,13
,119
,则应将其与其他列组合。也就是说,group by id\u product,someOtheCol
不使用WHERE FIND\u IN\u SET(p.id\u product,$id\u prod\u array)>0
返回空白结果检查一次,其中包含测试数据,例如,如果p.id\u product
是89
WHERE FIND\u IN\u SET(89,'25,62,89,123,12,25,62'))并查看是否可以获得结果。检查我的更新并更正。我删除了CSV字符串中的空格。WHERE FIND_in_SET(89,REPLACE('25,62,89,123,12,25,25,62','')
WHERE FIND_in_SET(89,25,62,89,123,12,25,25,62')返回表中的所有行。更正后返回空白结果。。。