MySQL:使用上一个查询的结果创建具有多个SELECTS的查询

时间:2018-12-11 14:25:56

标签: php mysql sql

我想检索具有特定值的行数,但是此值取决于在使用此结果之前必须执行的查询

已知变量为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 |

1 个答案:

答案 0 :(得分:1)

使用子查询获取此信息

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