我想检索具有特定值的行数,但此值取决于在使用此结果之前必须完成的查询
已知变量是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 |

最佳答案

使用子查询获取此

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

10-07 16:02