我有两个表,想要根据 category_id
查询和合并它们。
我的事件表有一个名为 event_category_id
的列,它在事件与其类别之间创建关系。该值是一个整数。
在我的类别表中,有一个名为 category_id
的列,这是我想要匹配的列,并将 int 值替换为 category_name
的 varchar 值。
我的语法是什么?感谢您的帮助!
类别
创建表
wp_wild_dbem_categories(
category_idint(11) NOT NULL auto_increment,
category_nametinytext NOT NULL,
主键(
category_id)
) ENGINE=MyISAM AUTO_INCRMENT=5 DEFAULT CHARSET=latin1
事件
创建表
wp_wild_dbem_events(
event_idmediumint(9) NOT NULL auto_increment,
event_authormediumint(9) 默认 NULL,
event_nametinytext NOT NULL,
event_start_time时间 NOT NULL 默认 '00:00:00',
event_end_time时间 NOT NULL 默认 '00:00:00',
event_start_date日期 NOT NULL 默认 '0000-00-00',
event_end_date日期默认为NULL,
event_notes文本,
event_rsvptinyint(1) NOT NULL 默认“0”,
event_seatstinyint(4) 默认 NULL,
event_contactperson_idmediumint(9) 默认 NULL,
location_idmediumint(9) NOT NULL 默认“0”,
recurrence_idmediumint(9) 默认 NULL,
event_category_idint(11) 默认 NULL,
唯一键
event_id(
event_id)
) ENGINE=MyISAM AUTO_INCRMENT=26 DEFAULT CHARSET=latin1
最佳答案
我认为您不需要UNION
。您可能想要这样的东西:
SELECT e.event_id, e.other_field, c.category_name
FROM events e JOIN categories c ON (e.category_id=c.category_id)
WHERE some_condition;
关于MySQL UNION 查询语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2267797/