i have two tables:
1. movie
id, userid, movie_id, status, score
2. movie_data
id, name_de, name_en, description, url
When i use this query:
SELECT *
FROM `movie` LEFT JOIN
movie_data ON movie.movie_id = movie_data.id
ORDER BY movie.id
When i enter this query, i get all fields, but also two times id. But i don't manage to show only the first "id" or rename the second id. i hope someone can help me :) Thanks for reading
You need to enumerate the columns in the SELECT
clause and use aliases to disambiguate homonym columns:
SELECT
m.id,
m.user_id,
m.movie_id,
m.status,
m.score,
d.id data_id, --> column alias
d.name_de,
d.name_en,
d.description,
d.url
FROM movie m
LEFT JOIN movie_data d ON m.movie_id = d.id
ORDER BY m.id
Explicitly list the columns you want. Don't use select *
:
SELECT m.id, m.userid, m.movie_id, m.status, m.score,
md.name_de, md.name_en, md.description, md.url
FROM `movie` m LEFT JOIN
movie_data md
ON m.movie_id = md.id
ORDER BY m.id
You can also do, for example
SELECT
`movie`.*, -- select all fields from one table
movie_data.id as movieId -- and some fields of another table
FROM `movie` LEFT JOIN
movie_data ON movie.movie_id = movie_data.id
ORDER BY movie.id
The query should be next:
SELECT
`movie`.`id`,
`movie`.`userid`,
`movie`.`movie_id`,
`movie`.`status`,
`movie`.`score`,
`movie_data`.`name_de`,
`movie_data`.`name_en`,
`movie_data`.`description`,
`movie_data`.`url`
FROM `movie`
LEFT JOIN `movie_data` ON `movie`.`movie_id` = `movie_data`.`id`
ORDER BY `movie`.`id`;
This way you can exactly select what you need
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.