简体   繁体   English

如何使用mysql连接具有相同属性但每个表中数据不同的2个表

[英]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.

相关问题 如何使用 PHP 加入具有每个表的特定 ID 的 MySQL 表 - How to JOIN MySQL tables with specific ID of each table with PHP 如何在mysql中加入4个表来获取同一个html表上的不同colums数据? - How to join 4 tables in mysql for getting different colums data on same html table? mySQL:如何遍历3个不同的表,引用每个表中的不同列,但使用主键 - mySQL: how to go along 3 different tables, referring to different column in each table but using primary keys SELECT 来自两个表 WHERE 每个表中的不同列等于 $id ORDER BY 公共列(PHP / MySQL) - SELECT from two tables WHERE different columns in each table equal $id ORDER BY common column (PHP/MySQL) MySQL:如何将数据从2个MySQL表连接到1个单个HTML表中 - MySQL: How to join data from 2 MySQL tables into 1 single HTML table Mysql在使用JOIN时为每个表设置了不同的选择集 - Mysql a different selection set for each table when using JOIN 如何使用同一列但关联不同的表多次联接一个表? (MySQL的) - How to join a table multiple times using same column but related different tables? (Mysql) PHP Codeigniter MySQL 查询 - 将 4 个表连接在一起,其中 3 个表使用每个表中的一列分组 - PHP Codeigniter MySQL query - join 4 tables together, with 3 tables using group by one column from each table mysql中具有不同列数的表的连接限制 - Join limit in mysql for tables having different column count PHP / MySQL - 使用JOIN从不同的表中获取数据 - PHP/MySQL - Using JOIN to fetch data from different table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM