I am trying to join two tables like this…
SELECT * FROM lists l
INNER JOIN products p USING (list_id)
Here is the show create table for both the tables..
CREATE TABLE `lists` (
`list_id` bigint(20) NOT NULL AUTO_INCREMENT,
`list_name` varchar(255) NOT NULL,
`deleted` tinyint(4) NOT NULL DEFAULT '0'
PRIMARY KEY (`list_id`),
KEY `index1` (`list_name`),
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
CREATE TABLE `products` (
`product_id` bigint(20) NOT NULL AUTO_INCREMENT,
`list_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`deleted` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`product_id`),
KEY `index2` (`deleted`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
Here’s the result of above query:
l.list_id : l.list_name : l.deleted : p.product_id : p.deleted
1 Christmas 0 1 1
1 Christmas 0 2 0
1 Christmas 0 3 0
1 Christmas 0 4 0
4 New Year 0 8 0
4 New Year 0 9 0
4 New Year 0 10 0
You see l.deleted column there with all zeros? In the table lists there are some rows with other values like 1, 2, … in deleted column. But joining the tables always show the deleted column to have all zeros.
Why is this happening? What am I missing here?
I suggest you use a different syntax to express your
JOIN. If you use theONsyntax, you’ll have more clarity about what is joining to what. This is stylistic.Try this:
But, I suspect you have items in your
liststable (withdeletedcolumn values that aren’t zero, that aren’t turning up in yourproductstable. To see those in your result set try this: