Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to combine UPDATE and COUNT in a query

How to combine UPDATE and COUNT in a query

Scheduled Pinned Locked Moved Database
databasehelptutorialannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joe Smith IX
    wrote on last edited by
    #1

    Hi, Could someone please help me in this simple task. I have two tables below. I need to set checked="ok" for those products that have no entry in [expiration date] table. Thanks in advance. I imagine this would be something like this, but it's incorrect: UPDATE (product as p INNER JOIN [expiration date] AS ed ON p.id=ed.product_id) SET checked='ok' WHERE (COUNT(*) FROM [expiration date] = 0) product ------- id name checked expiration date --------------- product_id exp_date quantity

    A K 2 Replies Last reply
    0
    • J Joe Smith IX

      Hi, Could someone please help me in this simple task. I have two tables below. I need to set checked="ok" for those products that have no entry in [expiration date] table. Thanks in advance. I imagine this would be something like this, but it's incorrect: UPDATE (product as p INNER JOIN [expiration date] AS ed ON p.id=ed.product_id) SET checked='ok' WHERE (COUNT(*) FROM [expiration date] = 0) product ------- id name checked expiration date --------------- product_id exp_date quantity

      A Offline
      A Offline
      Arun Immanuel
      wrote on last edited by
      #2

      Hope this helps U. Test this with some fake table before U proceed. update product set ckecked='ok' where id not in (select product_id from expirationdate) Regards, Arun Kumar.A -- modified at 10:43 Sunday 22nd April, 2007

      1 Reply Last reply
      0
      • J Joe Smith IX

        Hi, Could someone please help me in this simple task. I have two tables below. I need to set checked="ok" for those products that have no entry in [expiration date] table. Thanks in advance. I imagine this would be something like this, but it's incorrect: UPDATE (product as p INNER JOIN [expiration date] AS ed ON p.id=ed.product_id) SET checked='ok' WHERE (COUNT(*) FROM [expiration date] = 0) product ------- id name checked expiration date --------------- product_id exp_date quantity

        K Offline
        K Offline
        kubben
        wrote on last edited by
        #3

        Try this: update p set checked='ok' from product p where id in ( select id from product p2 join expirationdate e on p2.id = e.product_id group by id having count(*) = 0) I guess the question would be if there are records or not, it might be easier to do a left join if there are no records. So that would be: update p set checked='ok' from product p where id in ( select id from product p2 left join expirationdate e on p2.id = e.product_id where e.product_id is null) Hope that helps. Ben

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups