Показать полную графическую версию : COUNT(*)
Можно ли в одном запросе подсчитать строки сразу в нескольких таблицах, например в четырёх?
Vlad Drakula
20-08-2005, 23:07
slaine
естли допустимо стобы результат выводился в разных строках, то без проблемм...
Vlad Drakula
21-08-2005, 00:02
slaine
тогда можно смотреть в сторону UNION...
vadimiron
21-08-2005, 00:04
slaine
Речь идёт о MySQL
Насколько я понимаю, то нельзя
Почитайте тут (http://dev.mysql.com/doc/mysql/en/counting-rows.html) , в комментарии к этой статье написано
If you are using SELECT DISTINCT on a complex join clause, you might be stumped to find the count without returning the whole recordset. For example:
SELECT DISTINCT COUNT(*) AS theCount FROM table1, table2 WHERE table1.user_id = table2.user_id
Will not return the same number for 'theCount' as you would have using mysql_num_rows and:
SELECT DISTINCT table1.user_id FROM table1, table2 WHERE table1.user_id = table2.user_id
This is because DISTINCT affects the user_id, not the count. If you try something like:
SELECT DISTINCT table1.user_id, COUNT(*) AS theCount FROM table1, table2 WHERE table1.user_id = table2.user_id
Then you'll get an error saying there's no GROUP BY clause. The key is to collapse all of the rows using GROUP BY NULL:
SELECT DISTINCT table1.user_id, COUNT(*) AS theCount FROM table1, table2 WHERE table1.user_id = table2.user_id GROUP BY NULL
Круто.. я делал точно так же, как в этой статье.
Vlad Drakula
21-08-2005, 11:33
vadimiron
не вводи человека в заблуждение... (веть сказано что можно в разных строках!)
slaine
в SQL есть оператор UNION он вам поможет...
© OSzone.net 2001-2012
vBulletin v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.