views:

81

answers:

4

I have this query:

SELECT mt.*, fordon.*, boende.*, elektronik.*, business.*, hem_inredning.*, hobby.*
FROM classified mt 
LEFT JOIN fordon ON fordon.classified_id = mt.classified_id 
LEFT JOIN boende ON boende.classified_id = mt.classified_id 
LEFT JOIN elektronik ON elektronik.classified_id = mt.classified_id 
LEFT JOIN business ON business.classified_id = mt.classified_id 
LEFT JOIN hem_inredning ON hem_inredning.classified_id = mt.classified_id 
LEFT JOIN hobby ON hobby.classified_id = mt.classified_id 
ORDER BY modify_date DESC

I need to implement a count here, to just count all rows in combination with the JOINS you see.

How should I do this?

SELECT COUNT(mt.*, fordon.* etc) FROM ? // This method wont work

Thanks

A: 
SELECT COUNT(*) FROM (SELECT mt.*, fordon.*, boende.*, elektronik.*, business.*,     hem_inredning.*, hobby.*
FROM classified mt 
LEFT JOIN fordon ON fordon.classified_id = mt.classified_id 
LEFT JOIN boende ON boende.classified_id = mt.classified_id 
LEFT JOIN elektronik ON elektronik.classified_id = mt.classified_id 
LEFT JOIN business ON business.classified_id = mt.classified_id 
LEFT JOIN hem_inredning ON hem_inredning.classified_id = mt.classified_id 
LEFT JOIN hobby ON hobby.classified_id = mt.classified_id) As A
Leslie
That is a bad idea! It would result in a huge temporary table from which you do not need any data.
newtover
+4  A: 

I removed the ORDER BY, as it is not required for the COUNT:

SELECT count(*)
FROM classified mt  
LEFT JOIN fordon ON fordon.classified_id = mt.classified_id  
LEFT JOIN boende ON boende.classified_id = mt.classified_id  
LEFT JOIN elektronik ON elektronik.classified_id = mt.classified_id  
LEFT JOIN business ON business.classified_id = mt.classified_id  
LEFT JOIN hem_inredning ON hem_inredning.classified_id = mt.classified_id  
LEFT JOIN hobby ON hobby.classified_id = mt.classified_id 
RedFilter
And lastly, how do I retrieve the nr of rows? $res=mysql_query($query); Then what do I do?
Camran
@Camran: it will return a table with a single row and a single column.
newtover
please guys, the title says "rookie". Give me the code...
Camran
you can add an alias to the count results and access just like any other field in a query:SELECT Count(*) As NumberOfRecs FROM ....
Leslie
A: 

How about simply:

SELECT COUNT(*) FROM ...
Rodin
A: 

How about SELECT COUNT(*) FROM ... ? I'm not sure what you're trying to count.

treznik
I am trying to count nr of rows found... Need it for paging... Why are the answers so different?
Camran
@Camran, are you trying to count everything in those tables? Or just a certain table? If trying to count all, SELECT COUNT(*) works
ggfan
No, not all, only rows which are JOINED, check my Query
Camran
Well `COUNT(*)` counts the combination of results between the joined tables. If you need it for paging you probably just need `SELECT COUNT(*) FROM mt` to get the count separately from the main JOIN query.
treznik
@skidding: I agree, @Camran does not need to make JOINs for counting since all of them are left joins (assuming that there is not more than a single corresponding row in each joined table)
newtover