Gran Giddens writes:
>SELECT table1.title, table2.feature FROM table1,
>table2 WHERE (table1.sku = $table2.sku) AND table1.sku
>in ($sku1, $sku2, $sku3) ORDER BY FIELD(table1.sku,
>$sku1, $sku2, $sku3) ASC
...
>How can I run my query to get 3 results and if the
>feature is missing still return the table.title and
>NULL for the feature?

This is a job for 'LEFT JOIN' :)  Given this data from your described
tables:

mysql> select * from table1;
+------+-------+
| sku  | title |
+------+-------+
|    1 | A     |
|    2 | B     |
|    3 | C     |
+------+-------+
3 rows in set (0.00 sec)

mysql> select * from table2;
+------+---------+
| sku  | feature |
+------+---------+
|    1 | a       |
|    1 | aa      |
|    2 | b       |
|    2 | bb      |
|    2 | bbb     |
+------+---------+
5 rows in set (0.00 sec)

SELECT table1.title, table2.feature
    FROM table1 LEFT JOIN table2 using (sku)
    WHERE table1.sku in (1, 2, 3)
    ORDER BY FIELD(table1.sku, 1, 2, 3) ASC

mysql> SELECT table1.title, table2.feature
    ->     FROM table1 LEFT JOIN table2 using (sku)
    ->     WHERE table1.sku in (1, 2, 3)
    ->     ORDER BY FIELD(table1.sku, 1, 2, 3) ASC
    -> ;
+-------+---------+
| title | feature |
+-------+---------+
| A     | a       |
| A     | aa      |
| B     | bbb     |
| B     | b       |
| B     | bb      |
| C     | NULL    |
+-------+---------+
6 rows in set (0.04 sec)

Take a look at the manual for 'LEFT JOIN' to see where I
came up with this information.
               Brad Eacker ([EMAIL PROTECTED])

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to