MySQL UNION 查询语法
我有两个表想要根据 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 默认字符集=latin1
I have two tables that I want to query and merge based on a category_id
.
My events table has a column called event_category_id
that creates a relationship between the event and it's category. The value is an int.
In my categories table I have a column called category_id
which is what I want to match on and replace the int value with the varchar value of category_name
.
What's my syntax? Thanks for the help!
categories
CREATE TABLE
wp_wild_dbem_categories(
category_idint(11) NOT NULL auto_increment,
category_nametinytext NOT NULL,
category_id
PRIMARY KEY ()
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
events
CREATE TABLE
wp_wild_dbem_events(
event_idmediumint(9) NOT NULL auto_increment,
event_authormediumint(9) default NULL,
event_nametinytext NOT NULL,
event_start_timetime NOT NULL default '00:00:00',
event_end_timetime NOT NULL default '00:00:00',
event_start_datedate NOT NULL default '0000-00-00',
event_end_datedate default NULL,
event_notestext,
event_rsvptinyint(1) NOT NULL default '0',
event_seatstinyint(4) default NULL,
event_contactperson_idmediumint(9) default NULL,
location_idmediumint(9) NOT NULL default '0',
recurrence_idmediumint(9) default NULL,
event_category_idint(11) default NULL,
event_id
UNIQUE KEY(
event_id)
) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=latin1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您不需要
UNION
。你可能想要这样的东西:I don't think you want a
UNION
. You probably want something like this:如果我理解正确的话,这不是 UNION 而是 JOIN
It's not UNION but JOIN if I understood correctly