P
P
pillson2021-07-05 22:05:24
MySQL
pillson, 2021-07-05 22:05:24

Help with MySQL query?

Good evening. Need help with a request.

The car_mark table contains the id_car_mark field and the name field.

A query is obtained to get id_car_mark by name

select id_car_mark from car_mark where name='AC'

And the second query is obtained if id_car_mark is received in the first table

select * from car_model where id_car_mark='1'

How to combine this query through INNER so as not to write 2 queries, conditionally get id by name from the first table and find all models from second by received Id

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
John Didact, 2021-07-06
@pillson

SELECT cm.* 
FROM
car_mark mk 
LEFT JOIN car_model ml ON ml.id_car_mark = mk.id_car_mark
WHERE mk.name = 'AC'
provided that mk.id_car_markis a unique value. Otherwise, something like
SELECT ml.*
FROM car_model ml
WHERE ml.id_car_mark = (
 SELECT mk.id_car_mark
 FROM car_mark mk
 WHERE mk.name = 'AC'
)

A
Akina, 2021-07-05
@Akina

SELECT car_model.* 
FROM car_model 
JOIN car_mark ON car_model USING (id_car_mark)
WHERE car_mark.name='AC'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question