douhao5280 2017-01-06 19:45
浏览 44

将工作多查询MySQL转移到PHP

The following works as intended in MySQL (phpMyAdmin):

SET @randomComposer=(SELECT composer FROM compositions ORDER BY RAND() LIMIT 1);
SELECT * FROM compositions WHERE composer!='' AND composer=@randomComposer;

But I am unable to carry this over into PHP. Using query concatenation, syntax highlighting fails at the start of the SET… line and I get an error querying the database. How do I convince PHP to recognise the working MySQL?

(Research brought me to mysqli_multi_query, which I understand is the route I need to follow, but I first need to sort out PHP's aversion to SET.)

  • 写回答

1条回答 默认 最新

  • duan0417 2017-01-06 19:58
    关注

    This is how I would try to do it with one statement.

    SELECT compositions.* FROM compositions
    INNER JOIN
    (SELECT composer FROM compositions ORDER BY RAND() LIMIT 1) AS RandComposer
    ON compositions.composer = RandComposer.composer
    

    You may need to add the WHERE composer!='' to the subquery.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法