MySQL Add Data Based on Unique ID
I've been looking around for an answer to a MySQL Query question but have not been able to find an answer. Any help is appreciated... Ps. I'm a beginner.
Issue: I have two tables, one is a main table with several columns and a unique ID column (sequential numbers) which is also the key. This table has had some rows of data deleted that were not needed. So the remaining ID are for example 1,3,5,7 etc.
I also have a second table with two columns, one being unique IDs (key) and one with text. Both tables initially belonged to the same table, but the second one was extracted and stored while the first one was worked on; basically they have the same IDs (key). This table still has all IDs 1,2,3,4,5,6,7 etc.
I now want to add back the the column with text from the second table, and match it to the rows with the same ID on the first table. Any text from the second table that do not have a matching key with the first table should simply be omitted.
Table Illustration
Table 1
ID Field 1 (Here I want the text from Table 2; only matching IDs) 1 Bla 3 Bla 5 Bla 7 BlaTable 2
ID Text 1 Bla 2 Bla 3 Bla 4 Bla 5 Bla 6 Bla 7 开发者_如何学编程BlaI would greatly appreciate help with writing this query.
Thank you, Patrik
You can do this with a simple join. Rows that only exist with the same id in both tables will be retrieved:
SELECT t1.id, t2.text
FROM table1 t1
INNER JOIN table2 t2 ON (t2.id = t1.id)
精彩评论