1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

i need a small help on a query

Discussion in 'PHP' started by baris22, Feb 15, 2010.

  1. #1
    hello,

    i am trying for ages and i do not have any luck. i am trying to make a simple query.

    if only all item_done == "Yes" then do something. How can i know if all rows are "Yes". (all of them needs to be yes, i shouldn`t have any no)

    
    
    $query="SELECT * FROM item WHERE orderr_reference = '".$ref."' ";
    
    			$res=mysql_query($query);
    			while($row = mysql_fetch_array($res)) {
    			if ($row['item_done'] == "Yes") {
    			// do something
    			 }
    			}
    
    PHP:
     
    baris22, Feb 15, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi, try this:
    $query="SELECT *,
    IF(!((SELECT COUNT(*) FROM item)-(SELECT COUNT(*) FROM item WHERE item_done='Yes')),1,0) ALL_YES 
    FROM item 
    WHERE orderr_reference='{$ref}'";
    $res=mysql_query($query) or die(mysql_error());
                while($row = mysql_fetch_array($res)) {
                if (intval($row['ALL_YES'])) {
                // do something
                 }
                }
    
    PHP:
     
    koko5, Feb 16, 2010 IP
  3. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #3
    And what doesn't work in this code?
     
    s_ruben, Feb 16, 2010 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    @s_ruben he needs to know if there is any record with "No", it works, but doesn't returns desired result.

    OK, to simplify my previous query:
    
    $query="SELECT *,
    EXISTS(SELECT 1 FROM item WHERE orderr_reference='{$ref}' AND item_done='No') ALL_YES
    FROM item 
    WHERE orderr_reference='{$ref}'";
    $res=mysql_query($query) or die(mysql_error());
                while($row = mysql_fetch_array($res)) {
                if (intval($row['ALL_YES'])) {
                // do something
                 }
                }
    
    PHP:
    Edit: Should be EXISTS instead of NOT EXISTS -> the above code edited...
     
    Last edited: Feb 16, 2010
    koko5, Feb 16, 2010 IP
    baris22 likes this.
  5. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #5
    And you can use this code too:

    
    $query="SELECT * FROM item WHERE orderr_reference = '".$ref."' AND item_done='No' ";
    
    $res=mysql_query($query);
    
    if(mysql_num_rows($res)==0){
       // do something
    }
    
    Code (markup):
     
    s_ruben, Feb 16, 2010 IP
    baris22 likes this.
  6. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    thanks for help. i got it now.
     
    baris22, Feb 16, 2010 IP