It’s hard to summarize this question into one sentence. I have two tables – one of them is updated automatically and regularly by an external feed. The second table has extra information for some of the rows in the second table, identified by an unique key. Here’s an example:
Table1
id: int PRIMARY KEY
channel: varchar(255) UNIQUE KEY
name: varchar(255) NOT NULL
verified: tinyint(1) NOT NULL
Table2
id: int PRIMARY KEY
channel: varchar(255) UNIQUE KEY
extra_info: varchar(255)
Table1 is the main table. This is the table that is actually being selected. I would like the fields (other than id and channel) from Table2 to be moved to Table1 upon selection, as if all rows in Table2 that exists in Table1 are merged in upon selection.
For example, I want the information in these two tables to end up as JSON output like this
[{
"id": 5,
"channel": "bkids2",
"name": "Bkid",
"verified": true,
"extra_info": "Some extra information added using Table2"
},
...
]
I’m wondering what the fastest way to proceed is. Can I do this quickly using SQL syntax? I know some types of queries are very slow, so perhaps it would be faster to just fetch all data in both tables and perform the merging using the server side programming language?
I’m half way assuming the former is the correct answer, in which case: How do I even do this using MySQL? Subqueries? Joins? Union?
I have to admit I don’t fully understand how either of these technologies work or how fast/slow they operate, so thanks to anybody who can point me in the right direction.
Try this: