I am beginner in javascript and nodejs, and not having much knowledge of mysql.
These are the column names of my MySQL table whose name is cars
:
- Name
- model
- TransmissionType
- EngineSize
- VehicleSize
- BodyType
I want to randomly select Name
and model
from cars
, but the category must match which will be decided by the other four coloumns: TransmissionType
, EngineSize
, VehicleSize
and Body Type
, in such a way that randomly selected cars must share same values for engine
, TransmissionType
, VehicheSize
and BodyType
. Output should be in two rows only.
Engine size has values: 3.2, 3.5, 1.6, 5.2, 2.0, 2.2
TransmissionType has values: Manual
and Automatic
VehicleSize has values: Compact
, MidSize
and Large
.
BodyType has values: Car
, van
and truck
.
What query should I write to achieve this? Please help.
I did this, and the query is working fine, but there are too many sub-queries are running in it, is it possible to achieve this using not more den 2-3 sub-queries?
SELECT Name, model
FROM cars
WHERE EngineSize IN (SELECT * FROM (SELECT EngineSize FROM cars ORDER BY RAND( ) LIMIT 1 ) AS A)
AND TransmissionType IN (SELECT * FROM (SELECT TransmissionType FROM cars ORDER BY RAND( ) LIMIT 1 ) AS B)
AND BodyType IN (SELECT * FROM (SELECT BodyType FROM cars ORDER BY RAND( ) LIMIT 1 ) AS C)
AND VehicleSize IN (SELECT * FROM (SELECT VehicleSize` FROM cars ORDER BY RAND( ) LIMIT 1 ) AS D)
LIMIT 2