Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Selecting data from multiple tables all with same structure but different data

Ok, here is my dilemma I have a database set up with about 5 tables all with the exact same data structure. The data is separated in this manner for localization purposes and to split up a total of about 4.5 million records.

A majority of the time only one table is needed and all is well. However, sometimes data is needed from 2 or more of the tables and it needs to be sorted by a user defined column. This is where I am having problems.

data columns:

id, band_name, song_name, album_name, genre 

MySQL statment:

SELECT * from us_music, de_music where `genre` = 'punk' 

MySQL spits out this error:

#1052 - Column 'genre' in where clause is ambiguous 

Obviously, I am doing this wrong. Anyone care to shed some light on this for me?

like image 643
Jayrox Avatar asked Jan 03 '09 19:01

Jayrox


People also ask

How do you select data from one table is not in another?

select [ selecting columns] From table1 Right OUTER JOIN table2 ON(table1. SQL> select e. select [ selecting columns] From table1 Right OUTER JOIN table2 ON(table1. select column_name from table 1 full outer join table 2 on(connection); here all the data from table 1 and table 2 will get retrieved.

What is correct to fetch only records between two tables?

The SQL intersect operator allows us to get common values between two tables or views.

How do I select all fields except one in MySQL?

A MySQL SELECT statement is used to retrieve data from a MySQL table. To exclude certain column(s) from the SELECT statement result, you can omit the column/ field name from the query.


2 Answers

I think you're looking for the UNION clause, a la

(SELECT * from us_music where `genre` = 'punk') UNION (SELECT * from de_music where `genre` = 'punk') 
like image 97
Mihai Limbășan Avatar answered Sep 21 '22 10:09

Mihai Limbășan


It sounds like you'd be happer with a single table. The five having the same schema, and sometimes needing to be presented as if they came from one table point to putting it all in one table.

Add a new column which can be used to distinguish among the five languages (I'm assuming it's language that is different among the tables since you said it was for localization). Don't worry about having 4.5 million records. Any real database can handle that size no problem. Add the correct indexes, and you'll have no trouble dealing with them as a single table.

like image 21
Ned Batchelder Avatar answered Sep 21 '22 10:09

Ned Batchelder