dongtaogou6226 2014-08-28 22:51
浏览 6
已采纳

如果两个表中都有结果,则两个pdo在一个中选择

So i have a problem i have a user table and a website table i need to select a random user from the users table just there username were there is a row in the website table with there username.Is this possible ?

So i am getting a random user table

$stmt9 = $db->prepare('SELECT * FROM users WHERE coins >= ? ORDER BY RAND() LIMIT 1');
$stmt9->execute( array('1') ) ;
$row = $stmt9->fetch();

Now some how i need to grab a random website were the owner = there username like so

$stmt21 = $db->prepare('SELECT * FROM websites WHERE owner = ?  ORDER BY RAND() LIMIT 1');
$stmt21->execute( array($row['username']) ) ;
$row21 = $stmt21->fetch();

The problem i have is it will grab the username fine but then if the user has not submitted a website the second select will fail so some how i need to put both these select together. And grab a row of the website table were there is a row and were the coins in the user table is over 1.

  • 写回答

1条回答 默认 最新

  • douying6206 2014-08-28 22:58
    关注

    You could check if the first query returns an empty set, anyway if you want one query and you are not interested in the username you could use this:

    SELECT 
      * 
    FROM 
      websites w
    WHERE w.owner 
      IN (SELECT u.username FROM users u WHERE u.coins >= ?)  
    ORDER BY 
      RAND() 
    LIMIT 
      1
    

    If you want you can limit the username selected in the subquery adding the random order by and the limit clause.

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

报告相同问题?

悬赏问题

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