gpt4 book ai didi

mysql 使用 set FIELD 和 case 导致排序错误

转载 作者:行者123 更新时间:2023-11-29 10:22:11 28 4
gpt4 key购买 nike

我有以下变量和查询。由于某种原因,它以错误的顺序对结果进行排序。

$fieldlist = 'SMOOTH,EMBOSSED,AAR,EMBOSSED AAR';

$strap_query = $db1q->query("
SELECT id,name,tag,size,break,footage,coils_per_skid,core_size,grade,color,cost
FROM Plastic_Strapping
WHERE subcat=$subcat AND visibility='1'
ORDER BY case when tag in ('$fieldlist') then 0 else 1 end, FIELD(tag,'$fieldlist'), sort_order ASC")
or die ('Unable to execute query. '. mysqli_error($db1q));

订单应该是

  1. 流畅
  2. 浮雕
  3. AAR
  4. 压纹 AAR

但是,它按照以下不正确顺序对它们进行排序

  1. 流畅
  2. AAR
  3. 浮雕
  4. 压纹 AAR

知道为什么吗?

是因为EMBOSSED AAR中含有EMBOSSED吗?所以把它放在AAR之后?如果是这样,知道如何解决这个问题吗?

<小时/>

编辑

'SMOOTH','EMBOSSED','AAR','EMBOSSED AAR' 放置在 ORDER BY FIELD 部分是可行的;但是,我仍然希望能够通过变量来编辑它。以下内容不起作用。

$fieldlist = "'SMOOTH','EMBOSSED','AAR','EMBOSSED AAR'";

$strap_query = $db1q->query("SELECT id,name,tag,size,break,footage,coils_per_skid,core_size,grade,color,cost FROM Plastic_Strapping WHERE subcat=$subcat AND visibility='1' ORDER BY case when tag in ('$fieldlist') then 0 else 1 end, FIELD(tag, $fieldlist), sort_order ASC") or die ('Unable to execute query. '. mysqli_error($db1q));

抛出此错误

Unable to execute query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SMOOTH','EMBOSSED','AAR','EMBOSSED AAR'') then 0 else 1 end, FIELD(tag, 'SMOOTH'' at line 1

最佳答案

试试这个:

$fieldlist = 'SMOOTH,EMBOSSED,AAR,EMBOSSED AAR';

$query = "SELECT id,name,tag,size,break,footage,coils_per_skid,core_size,grade,
color,cost FROM Plastic_Strapping WHERE subcat = $subcat AND visibility='1'
ORDER BY FIELD( tag, '".implode("','",explode(',',$fieldlist))."' )";

更新

如果您的 $fieldlist 变量如下所示:

$fieldlist = "'SMOOTH','EMBOSSED','AAR','EMBOSSED AAR'";

您的查询将如下所示:

$query = "SELECT id,name,tag,size,break,footage,coils_per_skid,core_size,grade,
color,cost
FROM Plastic_Strapping WHERE subcat = $subcat AND visibility = '1'
ORDER BY case when tag in ($fieldlist) then 0 else 1 end,
FIELD(tag, $fieldlist), sort_order ASC ";

关于mysql 使用 set FIELD 和 case 导致排序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49052673/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com