Jump to content
  • 0

Выборка из двух таблиц not in


leonid26
 Share

Question

Есть две таблицы:


create table groups(
id int,
title char(64)
);

create table products(
group_id char(6),
title char(64)
);

Таблицы максимально упрощены.

Необходимо из таблицы products извлечь все записи для которых не найдено группы в таблице groups

Моё решение:

select title from products where group_id not in (select id groups);

возвращает неверные данные, а именно - если у есть группа с id 2 то ни одна запись из таблицы products содержащая 2 в group_id не будет возвращена....

подскажите варианты выхода их положения.

Edited by leonid26
Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

mysql> SELECT *, g.id FROM `products` AS p LEFT JOIN `groups` AS g ON p.group_id = g.id WHERE g.id IS NULL;
Empty set, 2637 warnings (0.19 sec)

mysql> SELECT title FROM products AS p WHERE NOT EXISTS (SELECT id FROM groups WHERE id = p.group_id);
Empty set, 2637 warnings (0.02 sec)

Конкретно таблицы выглядят так:

create table products (
id int not null primary key auto_increment,
group_id char(6) default 0,
title varchar(256) not null,
stok char(6),
price float not null,
pdate timestamp,
provider int not null
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

create table groups (
id int not null primary key auto_increment,
title char(128),
parent int not null default 0
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

Link to comment
Share on other sites

  • 0

mysql> SELECT *, g.id FROM `products` AS p LEFT JOIN `groups` AS g ON p.group_id = g.id WHERE g.id IS NULL;
Empty set, 2637 warnings (0.19 sec)

mysql> SELECT title FROM products AS p WHERE NOT EXISTS (SELECT id FROM groups WHERE id = p.group_id);
Empty set, 2637 warnings (0.02 sec)

Конкретно таблицы выглядят так:

create table products (
id int not null primary key auto_increment,
group_id char(6) default 0,
title varchar(256) not null,
stok char(6),
price float not null,
pdate timestamp,
provider int not null
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

create table groups (
id int not null primary key auto_increment,
title char(128),
parent int not null default 0
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

Может быть корень зла в этом: id таблицы groups - int, а group_id таблицы products - char(6)?

Или Вам категорически необходимо сравнение строки с числом?

Edited by notTrue
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy