[英]how to join 2 tables having equal attributes but different data in each table using mysql
Table 1 表格1
create table itemType1(
maincatg varchar(25),
subcatg varchar(25),
price float(5),
primary key(maincatg, subcatg)
);
Table2 表2
create table itemType2(
maincatg varchar(25),
subcatg varchar(25),
price float(5),
primary key(maincatg, subcatg)
);
It sounds like you're looking for a UNION
rather than a JOIN
. 听起来您正在寻找
UNION
而不是JOIN
。
(SELECT maincatg, subcatg, price
FROM itemType1)
UNION ALL
(SELECT maincatg, subcatg, price
FROM itemType2)
This is effectively a concatenation of the two tables. 这实际上是两个表的串联。
Beyond that, I'm really not sure what you're hoping to accomplish. 除此之外,我真的不确定您希望完成什么。
This kind of table is going to be much easier to work with. 这种表将更容易使用。
create table item(
maincatg varchar(25),
subcatg varchar(25),
price float(5),
type tinyint(1),
primary key(maincatg, subcatg)
);
It also looks like you're going to have a lot of duplicate data in your maincatg and subcatg columns. 看起来您的maincatg和subcatg列中将有很多重复数据。 If I was doing it I would have those in another table or possibly two.
如果我这样做的话,我会将它们放在另一个表或两个表中。 You join them with ids.
您使用ID加入他们。
You will probably have an easier time if you read up on database normalization. 如果您对数据库规范化有所了解,则可能会更轻松。 It's an easy concept to brush up on as long as you find a decent tutorial and there are plenty out there.
只要您找到了不错的教程并且那里有很多内容,就可以轻松上手。
If you're stuck using a database that you can't change for whatever reason, you should probably edit your question to say so, or else most answers will probably be similar to mine. 如果您因某种原因而无法使用无法更改的数据库,则可能应该编辑问题以这样说,否则大多数答案可能与我的相似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.