SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    counting rows in a table

    Hi,

    I have a mysql result which contains a number of rows fetched from 3 tables.

    So normally, if I want to count the number of row from mysql result and if its one table I would do this mysql_num_rows($result);

    Since I want to know the number of rows fetched from one of the tables, I can not do the code above as it will give the combined result of 3 tables.

    How would you do in this situation ?

  2. #2
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    Hi,

    I have a mysql result which contains a number of rows fetched from 3 tables.

    So normally, if I want to count the number of row from mysql result and if its one table I would do this mysql_num_rows($result);

    Since I want to know the number of rows fetched from one of the tables, I can not do the code above as it will give the combined result of 3 tables.

    How would you do in this situation ?
    Can you show us your SQL?

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes here.

    Code MySQL:
    "SELECT a.* 
    		, b.thumb_id
    		, c.amount
    	FROM listing a
    	LEFT OUTER 
    	JOIN listing_photo b
    	ON ( a.id = b.listing_id)
    	LEFT OUTER
    	JOIN listing_bid c
    	ON (a.id = c.listing_id)
    	WHERE a.activation = 1
    	AND b.key = 1"

    I want to count how many amount selected.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT a.* 
         , b.thumb_id
         , ( SELECT COUNT(*)
               FROM listing_bid 
              WHERE listing_id = a.id ) AS c_rows
         , c.amount
      FROM listing a
    LEFT OUTER 
      JOIN listing_photo b
        ON b.listing_id = a.id
       AND b.key = 1
    LEFT OUTER
      JOIN listing_bid c
        ON c.listing_id = a.id
     WHERE a.activation = 1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just looking at some random query you know exactly what to do with it. Genius.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    output rows from multiple table

    Hi,

    I have a query involved 3 tables, the number of rows pulled from 3 tables are different

    For instance, a gives me 1 row, b gives me 1 row, c gives me 10 rows

    How could I can output all c.amount ?

    Code MySQL:
    "SELECT a.* 
    			, b.thumb_id
    			, c.amount
    			, ( SELECT COUNT(*)
            		FROM listing_bid 
          		     WHERE listing_id = a.id ) AS c_rows	
    		FROM listing a
    		LEFT OUTER 
    		JOIN listing_photo b
    		  ON b.listing_id = a.id
    		 AND b.key = 1
    		LEFT OUTER
    		JOIN listing_bid c
    		  ON c.listing_id = a.id
    	   WHERE a.activation = 1";


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
  •