Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

MySQL query to JOIN tables based on column values

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.

Table 1 Contents

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

Table 2 Contents

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

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 *


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...