I have a table "move" like this
----------------------------------
| id | ... | ... | tab | idTab |
----------------------------------
| 1 | ... | ... | bike | 14 |
| 2 | ... | ... | car | 57 |
| 3 | ... | ... | car | 23 |
| 4 | ... | ... | bike | 43 |
| 5 | ... | ... | boat | 20 |
| .. | ... | ... | ... | .... |
----------------------------------
tab "bike"
---------------------------------
| id | code | name | matr | ... |
---------------------------------
| .. | ... | .... | .... | ... |
| 14 | AAA | MARIO | 111 | ... |
| .. | ... | .... | .... | ... |
| 43 | A1C | JOHN | EEE | ... |
| .. | ... | .... | .... | ... |
---------------------------------
tab "car"
---------------------------------
| id | code | name | matr | ... |
---------------------------------
| .. | ... | .... | .... | ... |
| 23 | 123 | JACK | WER | ... |
| .. | ... | .... | .... | ... |
| 57 | 2A2 | FRANK | MSS | ... |
| .. | ... | .... | .... | ... |
---------------------------------
tab "boat"
---------------------------------
| id | code | name | matr | ... |
---------------------------------
| .. | ... | .... | .... | ... |
| 20 | UJN | PETER | WSX | ... |
| .. | ... | .... | .... | ... |
---------------------------------
I would like to have in a single query all datas of "move" and the datas of each tables present in "move" with specific idTab.
The result will be like this
--------------------------------------------------------------
| id | ... | ... | tab | idTab | code | name | matr | ... |
--------------------------------------------------------------
| 1 | ... | ... | bike | 14 | AAA | MARIO | 111 | ... |
| 2 | ... | ... | car | 57 | 2A2 | FRANK | MSS | ... |
| 3 | ... | ... | car | 23 | 123 | JACK | WER | ... |
| 4 | ... | ... | bike | 43 | A1C | JOHN | EEE | ... |
| 5 | ... | ... | boat | 20 | UJN | PETER | WSX | ... |
| .. | ... | ... | ... | .... | ... | .... | .... | ... |
--------------------------------------------------------------
I don't know in effect if I have or not a specific table in move. I can make a query like this
SELECT * FROM move WHERE id>0
and then a foreach with
SELECT * FROM move["tab"] WHERE id=move["idTab"]
but is very hard work because table move is very long...
I try with this but (obviously) don't work...
SELECT M.*, T.* FROM move as M, M.tab as T WHERE M.id>0 AND M.idTab=T.id
Your database design is not ideal, and I don't think you should be having separate tables for each type of vehicle. That being said, if you bring the three tables into a single one using a UNION
, then a single join could yield your desired result.
SELECT t1.*, t2.*
FROM move t1
INNER JOIN
(
SELECT id, code, name, matr, 'bike' AS tab
FROM bike
UNION ALL
SELECT id, code, name, matr, 'car'
FROM car
SELECT id, code, name, matr, 'boat'
FROM boat
) t2
ON t1.idTab = t2.id AND
t1.tab = t2.tab
I added a computed column for the type of table under the assumption that perhaps the same id
could appear in more than one vehicle table.
Select 'bike' vehicle,column1,.... from bike
Union
Select 'boat',column1,... from boat
Union...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.