SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    COUNT and LISTING in same query

    Is this possible to do? I thought I did it before..

    I cant figure out why this will only list one category when i place the COUNT() item there.

    If I change it to l.listing_id AS total it will list all 5 items though,

    Am i doing something very wrong??

    Code MySQL:
    SELECT		c.category_id, 
    			c.title,
     
    		-- WHY IS THIS BELOW CAUSING ONLY 1 TO LIST???
    		-- CUZ THERE ARE 5 REALLY, THEYLL WORK IF I TAKE OFF COUNT
    			COUNT(l.listing_id) AS total
     
    FROM		categories AS c
     
    LEFT JOIN listings AS l
    ON
    (
    	l.assign_category = c.category_id
    )
     
    ORDER BY	title ASC

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    you're using an aggregate function without a GROUP BY clause, that's why

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So THATS what aggregate means LOL

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    so could i see the query after you added the GROUP BY clause?

    i want to talk a bit more about aggregates
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2007
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like the gravel and stuff in concrete?

  6. #6
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heya,

    Sorry I thought this thread died lol, here is my query.


    Code MySQL:
    SELECT		c.category_id, 
    			c.title,
    			c.description,
    			COUNT(l.listing_id) AS total
     
    FROM		categories AS c
     
    LEFT JOIN listings AS l
    ON
    (
    	l.assign_category = c.category_id
    	AND
    	l.active = 1
    )
     
     
    GROUP BY c.title

    Im not sure for the reason of it working, but it works

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    i forgot what i wanted to say

    your query looks fine
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hahaha


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •