我有这三张桌子。
channel_contacts与频道的关系
PRIMARY KEY (`id`),
UNIQUE INDEX `CONTRAINT` (`id_channel`, `id_contact`),
INDEX `idx_channel_contact_archived` (`archived`),
INDEX `channel_contacts_ibfk_2` (`id_contact`),
CONSTRAINT `channel_contacts_ibfk_1` FOREIGN KEY (`id_channel`) REFERENCES `channels` (`id`) ON DELETE CASCADE,
CONSTRAINT `channel_contacts_ibfk_2` FOREIGN KEY (`id_contact`) REFERENCES `contact` (`id_contact`) ON DELETE SET NULL
channel_events与channel_contacts的关系
PRIMARY KEY (`id`),
INDEX `id_channel_contacts` (`id_channel_contacts`),
CONSTRAINT `channel_events_ibfk_1` FOREIGN KEY (`id_channel_contacts`) REFERENCES `channel_contacts` (`id`) ON DELETE CASCADE
可以看出它们是相关的。
我希望来自channels_events表的所有那些属于通道表中id_account 123的事件。
我写了以下查询,它给了我以下
的问题未知栏' channel_contacts.id_channel'在' on条款'
SELECT channels.id, channels.kind, channels.publish_date,
channel_contacts.id_channel, channel_contacts.id_contact,
channel_contacts.archived, channel_events.id,
channel_events.id_channel_contacts, channel_events.code,
channel_events.details, channel_events.occation
FROM channel_events
JOIN channels
ON channels.id = channel_contacts.id_channel
JOIN channel_contacts
ON channel_contacts.id_contact = channel_events.id_channel_contacts
where channels.id_account = 123;
答案 0 :(得分:2)
我认为这是一个错字,您已经在频道联系人之前将频道表加入了频道表,因此尚未识别channels_contanct。试试这个:
SELECT channels.id, channels.kind, channels.publish_date, channel_contacts.id_channel, channel_contacts.id_contact, channel_contacts.archived, channel_events.id, channel_events.id_channel_contacts, channel_events.code, channel_events.details, channel_events.occation
FROM channel_events
JOIN channel_contacts ON channel_contacts.id= channel_events.id_channel_contacts
JOIN channels ON channels.id = channel_contacts.id_channel
where channels.id_account = 123;