gpt4 book ai didi

php - 从字符串中的 ids 中检索表中的随机行

转载 作者:行者123 更新时间:2023-11-29 13:24:59 25 4
gpt4 key购买 nike

我正在尝试从表中随机选择一行,其 id 位于数组中。

字符串 $data 包含我想要用逗号分隔的所有 id 列表。

$statement = $conn->prepare("SELECT * FROM clients ORDER BY rand() WHERE find_in_set(id,'$data') LIMIT 1");

这是我的查询,

id 是我的表的字段。

这是错误

An error has occurred exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'WHERE find_in_set(id,'0,1') LIMIT 1' at line 1' in 

最佳答案

试试这个,ORDER BY应该在where条件或tablename之后

SELECT * FROM clients  WHERE find_in_set(id,'$data') ORDER BY rand() LIMIT 1

语法:http://dev.mysql.com/doc/refman/5.0/en/select.html

 SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]

关于php - 从字符串中的 ids 中检索表中的随机行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20262275/

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