sql query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gm000
    New Member
    • Nov 2009
    • 11

    #1

    sql query

    i have two tables one submittable and 2nd commenttabe
    submittabls has field
    Code:
    ID
    URLID
    URLADDRESS
    USERNAME
    DATETIME
    2nd table(commentta ble)
    Code:
    URLID
    COMMENTID
    TEXT
    Now i want single query which show submittable records and count commentid form commenttable.

    Please help me.
    Last edited by Atli; Nov 10 '09, 11:15 AM. Reason: Fixed the formatting.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    To do that, you could use either a JOIN or a Subquery.

    For example, say I had a News table and a NewComment table, where the comments were link to the news by a Foreign Key on the News id column.

    Using a Subquery, I could get the news info and count the number of comments for it by doing:
    [code=sql]
    SELECT
    n.`id`, n.`headline`, n.`body`, n.`author`,
    (SELECT COUNT(*)
    FROM `news_comments` AS nc
    WHERE nc.`news_id` = n.`id`
    ) AS 'comments'
    FROM `news` as n
    [/code]

    Comment

    Working...