This feels like it should be really easy, but I just can’t get it and I’ve tried everything.
How can I just get the NUMBER OF ROWS returned by this query:
(SELECT Alias1 . *,
Alias2 . *
FROM Table1 AS Alias1
LEFT JOIN Table2 AS Alias2 USING ( Col1 )
WHERE Alias1.Col1 != 'blah1'
AND Alias1.Col2 LIKE 'blah2'
AND ( Alias1.Col3 LIKE 'blah3'
OR Alias1.Col3 LIKE 'blah4' )
AND Alias1.Col4 = 'blah5'
AND Alias2.Col7 LIKE 'blah6'
ORDER BY Alias1.Col6 DESC
LIMIT 50)
UNION
(SELECT Alias1 . *,
Alias2 . *
FROM Table1 AS Alias1
LEFT JOIN Table2 AS Alias2 USING ( Col1 )
WHERE Alias1.Col1 != 'blah1'
AND Alias1.Col2 LIKE 'blah2'
AND ( Alias1.Col3 LIKE 'blah3'
OR Alias1.Col3 LIKE 'blah4' )
AND Alias1.Col4 = 'blah5'
AND Alias2.Col5 LIKE 'blah6'
ORDER BY Alias1.Col6 DESC
LIMIT 50)
UNION
(SELECT Alias1 . *,
Alias2 . *
FROM Table1 AS Alias1
LEFT JOIN Table2 AS Alias2 USING ( Col1 )
WHERE Alias1.Col1 != 'blah1'
AND Alias1.Col2 LIKE 'blah2'
AND ( Alias1.Col3 LIKE 'blah3'
OR Alias1.Col3 LIKE 'blah4' )
AND Alias1.Col4 = 'blah5'
ORDER BY Alias1.Col6 DESC
LIMIT 50)
I suppose I could just use PHP to execute the query and then do a mysql_num_rows on the results, but I want to do it directly with SQL because I heard that’s faster as it saves a step.
Thanks!
EDIT:
The following is giving me error (#1060 – Duplicate column name ‘Col1’):
SELECT COUNT(*) FROM (
(SELECT Alias1 . *,
Alias2 . *
FROM Table1 AS Alias1
LEFT JOIN Table2 AS Alias2 USING ( Col1 )
WHERE Alias1.Col1 != 'blah1'
AND Alias1.Col2 LIKE 'blah2'
AND ( Alias1.Col3 LIKE 'blah3'
OR Alias1.Col3 LIKE 'blah4' )
AND Alias1.Col4 = 'blah5'
AND Alias2.Col7 LIKE 'blah6'
ORDER BY Alias1.Col6 DESC
LIMIT 50)
UNION
(SELECT Alias1 . *,
Alias2 . *
FROM Table1 AS Alias1
LEFT JOIN Table2 AS Alias2 USING ( Col1 )
WHERE Alias1.Col1 != 'blah1'
AND Alias1.Col2 LIKE 'blah2'
AND ( Alias1.Col3 LIKE 'blah3'
OR Alias1.Col3 LIKE 'blah4' )
AND Alias1.Col4 = 'blah5'
AND Alias2.Col5 LIKE 'blah6'
ORDER BY Alias1.Col6 DESC
LIMIT 50)
UNION
(SELECT Alias1 . *,
Alias2 . *
FROM Table1 AS Alias1
LEFT JOIN Table2 AS Alias2 USING ( Col1 )
WHERE Alias1.Col1 != 'blah1'
AND Alias1.Col2 LIKE 'blah2'
AND ( Alias1.Col3 LIKE 'blah3'
OR Alias1.Col3 LIKE 'blah4' )
AND Alias1.Col4 = 'blah5'
ORDER BY Alias1.Col6 DESC
LIMIT 50)
) a
The following is giving me error (#1064 – You have an error in your SQL syntax; ):
SELECT COUNT(*) FROM (
(SELECT Alias1 . *,
Alias2 . *
FROM Table1 AS Alias1
LEFT JOIN Table2 AS Alias2 USING ( Col1 )
WHERE Alias1.Col1 != 'blah1'
AND Alias1.Col2 LIKE 'blah2'
AND ( Alias1.Col3 LIKE 'blah3'
OR Alias1.Col3 LIKE 'blah4' )
AND Alias1.Col4 = 'blah5'
AND Alias2.Col7 LIKE 'blah6'
ORDER BY Alias1.Col6 DESC
LIMIT 50) a
UNION
(SELECT Alias1 . *,
Alias2 . *
FROM Table1 AS Alias1
LEFT JOIN Table2 AS Alias2 USING ( Col1 )
WHERE Alias1.Col1 != 'blah1'
AND Alias1.Col2 LIKE 'blah2'
AND ( Alias1.Col3 LIKE 'blah3'
OR Alias1.Col3 LIKE 'blah4' )
AND Alias1.Col4 = 'blah5'
AND Alias2.Col5 LIKE 'blah6'
ORDER BY Alias1.Col6 DESC
LIMIT 50) b
UNION
(SELECT Alias1 . *,
Alias2 . *
FROM Table1 AS Alias1
LEFT JOIN Table2 AS Alias2 USING ( Col1 )
WHERE Alias1.Col1 != 'blah1'
AND Alias1.Col2 LIKE 'blah2'
AND ( Alias1.Col3 LIKE 'blah3'
OR Alias1.Col3 LIKE 'blah4' )
AND Alias1.Col4 = 'blah5'
ORDER BY Alias1.Col6 DESC
LIMIT 50) c
) z
You need to specify a unique alias for your Column
Col1since it is used in ajoinwhich requires unique columns to match against each other. It will throw an error 1060 if it comes across duplicated columns. Check this: Duplicate column names in SQL queryIn your case, the MySQL gets confused with
Col1in table 1 and table 2 and hence the error. Once you have used that, you canSELECT COUNT(*) from YourQuery