Here are my tables:
mysql> describe christmas_names;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| name_id | smallint(3) | | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> describe christmas_gifts;
+---------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+----------------+
| gift_id | tinyint(2) unsigned | | PRI | NULL | auto_increment |
| name_id | varchar(25) | | | | |
| gift | text | | | | |
+---------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from christmas_names;
+---------+-----------+
| name_id | name |
+---------+-----------+
| 1 | Jim |
| 2 | Joe |
| 3 | Kathy |
| 4 | Sallye |
+---------+-----------+
4 rows in set (0.01 sec)
mysql> select * from christmas_gifts;
+---------+---------+-----------------------------------+
| gift_id | name_id | gift |
+---------+---------+-----------------------------------+
| 1 | 1 | Drums and computer stuff. |
| 2 | 4 | Some books from Barnes and Noble. |
+---------+---------+-----------------------------------+
I'm trying to select all the names that HAVEN'T picked a gift yet. In other words, the names that are NOT in the christmas_gift table.
I thought that this would work but it doesn't.
SELECT t_names.name
FROM
christmas_names t_names,
christmas_gifts t_gifts
WHERE
t_names.name_id != t_gifts.name_id
ORDER BY t_names.name
However, I can successfully select everyone that HAS chose a gift. (but I need the opposite of this).
mysql> select t_names.name
-> from christmas_names t_names, christmas_gifts t_gifts
-> where t_names.name_id = t_gifts.name_id
-> order by t_names.name;
+--------+
| name |
+--------+
| Jim |
| Sallye |
+--------+
2 rows in set (0.00 sec)
Or maybe there is a better way to do this?
Thanks for any help!
Jim
mysql> describe christmas_names;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| name_id | smallint(3) | | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> describe christmas_gifts;
+---------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+----------------+
| gift_id | tinyint(2) unsigned | | PRI | NULL | auto_increment |
| name_id | varchar(25) | | | | |
| gift | text | | | | |
+---------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from christmas_names;
+---------+-----------+
| name_id | name |
+---------+-----------+
| 1 | Jim |
| 2 | Joe |
| 3 | Kathy |
| 4 | Sallye |
+---------+-----------+
4 rows in set (0.01 sec)
mysql> select * from christmas_gifts;
+---------+---------+-----------------------------------+
| gift_id | name_id | gift |
+---------+---------+-----------------------------------+
| 1 | 1 | Drums and computer stuff. |
| 2 | 4 | Some books from Barnes and Noble. |
+---------+---------+-----------------------------------+
I'm trying to select all the names that HAVEN'T picked a gift yet. In other words, the names that are NOT in the christmas_gift table.
I thought that this would work but it doesn't.
SELECT t_names.name
FROM
christmas_names t_names,
christmas_gifts t_gifts
WHERE
t_names.name_id != t_gifts.name_id
ORDER BY t_names.name
However, I can successfully select everyone that HAS chose a gift. (but I need the opposite of this).
mysql> select t_names.name
-> from christmas_names t_names, christmas_gifts t_gifts
-> where t_names.name_id = t_gifts.name_id
-> order by t_names.name;
+--------+
| name |
+--------+
| Jim |
| Sallye |
+--------+
2 rows in set (0.00 sec)
Or maybe there is a better way to do this?
Thanks for any help!
Jim