douxi2670 2018-05-31 22:42
浏览 41
已采纳

MySQL SSH控制台正确运行子查询,但mysqli为子查询列返回零

I'm hesitant to post the entire query but perhaps it's necessary with my variable assignments. In short, the tenth column is a subquery and should return a count of occurrences for something in another table.

SQLFiddle: http://sqlfiddle.com/#!9/3718e/2

User ID 366 is a test production account. This is normally a dynamically-binded ? prepared variable. User ID 366 should return two rows. The nine columns aren't important for this question, but the 10th (the subquery) should have a count of 1 and 0 in the first and second rows, respectively. This works on an SSH connection to MySQL. However, PHP's mysqli and even PhpMyAdmin return 0 and 0 respectively. I have sandboxed the PHP so the error is lying in the SQL I believe.

Sample correct output from SSH (9 columns before ignored):

+--------+ | guests | +--------+ | 0 | | 1 | +--------+

mysqli will return just zeros. Other columns are unaffected and SSH/mysqli both agree and return correctly. If more context is needed, let me know.

Thank you.


Screenshot from server

SELECT tickets.id, @formal_id:=schedule.id as 'formal_id', schedule.name, schedule.date, schedule.colour, @li_price:=schedule.livers_in_price as 'li', @lo_price:=schedule.livers_out_price as 'lo', @gu_price:=schedule.guests_price as 'gu', tickets.for_sale as 'for_sale', @guest:=( SELECT COUNT(*) FROM billing WHERE guest = 1 AND user_id = @user_id AND formal_id = @formal_id ) AS 'guests' FROM tickets JOIN user ON tickets.user_id = user.user_id JOIN schedule ON tickets.formal_id = schedule.id JOIN billing ON tickets.formal_id = billing.formal_id WHERE user.user_id = 366 AND schedule.date > NOW() GROUP BY tickets.id;

  • 写回答

1条回答 默认 最新

  • dsa5233 2018-05-31 23:57
    关注

    without specifying the table source for a variable when joins are in play you can get unexpected results. Using table.column syntax will save you a lot of grief.

    In this case AND user_id = tickets.user_id

    but I cant explain the inconsistent results between your testing and mine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器