MySQL根据列值查询JOIN表 [英] MySQL query to JOIN tables based on column values

查看:101
本文介绍了MySQL根据列值查询JOIN表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个桌子.一个带有零件号,硬件名称和类型,另一个带有硬件的位置,也带有包含硬件的特定料仓的位置.垃圾箱没有特定的编号,但是具有唯一的名称.第二个表还具有随时间变化的硬件和存储箱的位置.而且我正在尝试创建一个MySQL查询,该查询将把数据合并到一个新表中,并以逗号分隔的文件形式输出.

I've got two tables. One with part numbers, hardware names, and type and other with the locations of the hardware that also has locations of specific bins that contain the hardware. The bins don't have a specific number but have unique names. The second table also has the location of the hardware and bin which may change over time. And I'm trying to create a MySQL query that will combine the data in a new table that will be outputted as a comma separated file.

表1内容

Part Number | Name          | Type
------------+---------------+---------------
0           | None          | Not Applicable
25          | name1         | type1
150         | name2         | type2

表2内容

Date     | Bin  |  Part Number | Event    | To Location | From Location
---------+------+--------------+----------+-------------+---------------
1/1/2013 | bin1 |  0           | arrive   | location1   | none
1/2/2013 | none |  25          | arrive   | location2   | none
1/2/2013 | none |  150         | relocate | location3   | location2

查询的最终输出应类似于:

The final output of the query should look something like:

Date     | Bin  | Part Number | Part Name | Type           | Event    | To Location | From Location
---------+------+-------------+-----------+----------------+----------+-------------+--------------
1/1/2013 | bin1 | 0           | None      | Not Applicable | arrive   | location1   | none
1/2/2013 | none | 25          | name1     | type1          | arrive   | location2   | none
1/2/2013 | none | 150         | name2     | type2          | relocate | location2   | location2

推荐答案

尝试一下:

SELECT
  *
FROM
  `Table1`
  INNER JOIN `Table2` ON (`Table1`.`Part Number`=`Table2`.`Part Number`)

为使查询更好,您需要定义要返回的所有列,而不是*

To make the query better, you would want to define all the columns that you wanted returned instead of *

这篇关于MySQL根据列值查询JOIN表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆