duan0802 2017-04-10 18:45
浏览 58
已采纳

MySQL中用户定义的@variable为空

I got problem with following query. When Im running it in mysql workbench, the user defined variable @cid is being filled correctly, however when i run it in PHP the variable is empty. Im using mysqli_query mechanism.

Its complicated query but as I have already said - works fine in mysql WB but not in PHP. In PHP count is being returned correctly so looks like whole query works except of storing of value in @cid variable.

Query:

SELECT count(*),@cid FROM users WHERE yfid IN 
(SELECT viewer_id 
FROM views WHERE campaign_id 
    IN (@cid:=(SELECT * FROM(SELECT campaign_id FROM views 
    where campaign_id>'$firstCampaignId' AND campaign_id 
    IN (SELECT id FROM campaigns WHERE owner_yfid 
    IN (SELECT id FROM uc_users WHERE email='$email')) 
group by campaign_id having count(*) > 1 
ORDER BY count(*) DESC LIMIT 1) AS tmp))) 
AND gender = 1 AND age < 35

EDIT: Ive found out that in MySQL workbench @cid is also NULL when Im running the query for the first time. When Im executing it for the second time, its returned correctly. So probably there is some problem with assigning logic? That is being assigned after excution of query and is accessible on second run?

  • 写回答

1条回答 默认 最新

  • drix47193 2017-04-10 21:03
    关注

    Try using

    select count(*), @cid:=V.campaign_id from
    ( select yfid from users
      where gender = 1 AND age < 35
    ) as U
    inner join
    ( select viewer_id, campaign_id FROM views 
      where campaign_id > '$firstCampaignId'
    ) as V on U.yfid = V.viewer_id
    inner join
    campaigns as C on V.campaign_id = C.id
    inner join
    ( select id from uc_users where email='$email' ) as UCU on C.owner_yfid = UCU.id
    group by V.campaign_id
    having count(*) > 1 
    order by count(*) desc limit 1
    

    I prefer putting where clauses in subqueries to reduce the number of rows actually joined, though I did not find any information on whether MySQL optimises this already.

    It does appear that MySQL optimises join statements but not in clauses.

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

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题