mysql:使用上一个查询的结果创建一个包含多个选择的查询

c9qzyr3d  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(206)

我想检索具有特定值的行数,但该值取决于在使用此结果之前必须执行的查询
已知变量为 adId ,例如 adId 是32所以我想得到 requestFrom 哪里 adId = 32 容易的: $result = SELECT requestFrom FROM myTable WHERE adId = 32 然后我得到计数:

SELECT COUNT(*) FROM myTable WHERE requestFrom = $result AND adId = $adId AND requestStatus = '1'

-我只想在一个查询中执行此操作(获取计数)

+----+--------+-------------+------+--------+
| id | userId | requestFrom | adId | status |
+----+--------+-------------+------+--------+
|  1 |     23 |          58 |   32 |      1 |
|  2 |     23 |          65 |   37 |      0 |
|  3 |     76 |          27 |   73 |      1 |
|  4 |     29 |          58 |   29 |      1 |
wb1gzix0

wb1gzix01#

使用子查询获取此

SELECT COUNT(*) FROM myTable 
WHERE requestFrom = (select requestFrom from myTable where adId = $adId ) 
AND adId = $adId AND requestStatus = '1'

相关问题