Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Selecting IDs that don't have any rows with a certain value for a column

Tags:

sql

php

I want to select the distinct IDs (which is associated with multiple rows) that DOESN'T have any rows where VAL = 'current'.

For example, in a table like this:

PK | ID | VAL 
-------------
 1 | 23 | deleted
 2 | 23 | deleted
 3 | 23 | deleted
 4 | 45 | current
 5 | 45 | deleted
 6 | 45 | deleted
...|................

I would want it to return ID 23, because it has no rows where VAL='current'. Note that in this table, the primary keys (PK) are unique, but IDs are not (hence the need to use DISTINCT or GROUP BY).

Here is what I have in PHP:

$conn = someConnect("");

// returns ids associated with the number of rows they have where VAL != 'current'
$sql = "SELECT id, count(*) FROM table WHERE val != 'current' GROUP BY id"

$stid = oci_parse($conn, $sql);
oci_execute($stid);

oci_fetch_all($stid, $arr, OCI_FETCHSTATEMENT_BY_ROW);

foreach ($arr as $elm) {
   $id = key($elm);
   $non_current_count = $elm[$id];

   // counts the number of rows associated with the id, which includes VAL = 'current' rows
   $sql2 = "SELECT count(*) FROM table WHERE id = $id";

   $stid2 = oci_parse($conn, $sql2);
   oci_execute($stid2);
   $total_count = oci_fetch_array...
   if ($total_count != $non_current_count) {
      $output[] = $id;
   } 
   ...
}

oci_close($conn);

That's the general gist of it. As you can see, it takes two SQL statements to accomplish this task. Is there a shorter way of doing this?

like image 699
George Newton Avatar asked Nov 28 '13 09:11

George Newton


People also ask

How do you select all records from one table that do not exist in another table Oracle?

How to Select All Records from One Table That Do Not Exist in Another Table in SQL? We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.

How do I select only certain rows in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.


2 Answers

SELECT DISTINCT id
FROM table
WHERE id NOT IN (SELECT id
                 FROM table
                 WHERE val = 'current')

or:

SELECT a.id
FROM table a
LEFT JOIN table b ON a.id = b.id AND b.val = 'current'
WHERE b.id IS NULL
like image 53
Barmar Avatar answered Oct 09 '22 00:10

Barmar


You can use having

SELECT
    id,
    count(*) as Total
FROM table
WHERE val <> 'current'
HAVING Total > 0

Output

| ID | TOTAL |
|----|-------|
| 23 |     5 |  

Fiddle

like image 1
Muhammad Raheel Avatar answered Oct 08 '22 23:10

Muhammad Raheel