我有这个SQL查询:
SELECT users.*, users_oauth.* FROM users LEFT JOIN users_oauth ON users.user_id = users_oauth.oauth_user_id WHERE (
(MATCH (user_email, user_firstname, user_lastname) AGAINST ('"+smith "+john"' IN BOOLEAN MODE)) )
ORDER BY user_date_accountcreated DESC LIMIT 0,50
有没有办法让此查询的COUNT(*)
忽略同一查询中的LIMIT 0,50
?
或者我必须做2个查询,一个用于结果,一个用于COUNT(*)?
感谢。
答案 0 :(得分:1)
如果您需要在每行上重复计数(*),您可以使用相关查询添加列
,则限制不是问题SELECT users.*, users_oauth.* , (select count(*) FROM users
LEFT JOIN users_oauth ON users.user_id = users_oauth.oauth_user_id
WHERE (
(MATCH (user_email, user_firstname, user_lastname) AGAINST ('"+smith "+john"' IN BOOLEAN MODE)) ) )
FROM users
LEFT JOIN users_oauth ON users.user_id = users_oauth.oauth_user_id
WHERE (
(MATCH (user_email, user_firstname, user_lastname) AGAINST ('"+smith "+john"' IN BOOLEAN MODE)) )
ORDER BY user_date_accountcreated DESC LIMIT 0,50
但是如果你只想在某些mysql版本中选择的列中添加count(*),你只能得到一行 而在其他情况下,你有错误,因为mixin聚合函数和单列没有声明group by子句
答案 1 :(得分:1)
如果您的MariaDB版本是10.2.0+,则支持窗口功能,您可以使用COUNT(*) OVER ()
:
SELECT
users.*,
users_oauth.*,
COUNT(*) OVER () AS countall
FROM users
LEFT JOIN users_oauth ON users.user_id = users_oauth.oauth_user_id
WHERE
MATCH (user_email, user_firstname, user_lastname) AGAINST ('"+smith "+john"' IN BOOLEAN MODE)
ORDER BY user_date_accountcreated DESC
LIMIT 0,50
如果您的MariaDB版本是10.2.0-(在此之前),您可以在列中使用嵌套选择:
SELECT
users.*,
users_oauth.*,
(SELECT count(*)
FROM users
LEFT JOIN users_oauth ON users.user_id = users_oauth.oauth_user_id
WHERE MATCH (user_email, user_firstname, user_lastname) AGAINST ('"+smith "+john"' IN BOOLEAN MODE)
) AS countall
FROM users
LEFT JOIN users_oauth ON users.user_id = users_oauth.oauth_user_id
WHERE
MATCH (user_email, user_firstname, user_lastname) AGAINST ('"+smith "+john"' IN BOOLEAN MODE)
ORDER BY user_date_accountcreated DESC
LIMIT 0,50