以下在MySQL(phpMyAdmin)中按预期工作:
SET @randomComposer=(SELECT composer FROM compositions ORDER BY RAND() LIMIT 1);
SELECT * FROM compositions WHERE composer!='' AND composer=@randomComposer;
但我无法将其转移到PHP中。使用查询连接,语法突出显示在SET ...行的开头失败,我收到查询数据库的错误。我如何说服PHP识别正在运行的MySQL?
(研究给我带来了mysqli_multi_query,我理解这是我需要遵循的路线,但我首先需要理解PHP对SET的厌恶。)
答案 0 :(得分:1)
这是我尝试用一个陈述来做的。
SELECT compositions.* FROM compositions
INNER JOIN
(SELECT composer FROM compositions ORDER BY RAND() LIMIT 1) AS RandComposer
ON compositions.composer = RandComposer.composer
您可能需要将WHERE composer!=''
添加到子查询中。