Home > DeveloperSection > Forums > SQL queries cleaner better in jquery
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    November-06-2014 3:39 AM

 MSSQL Server MYSQL  SQL Server  SQL 
Ratings:


 1 Reply(s)
 609  View(s)
Rate this:

I have something like this:

SELECT cars.brand, cars.id FROM cars WHERE cars.brand = 4 LIMIT 1;
SELECT cars.brand, cars.id FROM cars WHERE cars.brand = 24 LIMIT 1;
SELECT cars.brand, cars.id FROM cars WHERE cars.brand = 29 LIMIT 1;

If will be more queries it will be very long. How do this in the other way, in one line? But important is for me speed. cars.brand = X AND LIMIT X is a variable so it maybe change.



Simons Hood
Simons Hood

Total Post:70

Points:494
Posted on    November-06-2014 6:28 AM

You are worried about having to send so many queries one by one, so that would take a lot of time. You can make this one query instead:

SELECT cars.brand, cars.id FROM cars WHERE cars.brand = 4 LIMIT 1
UNION ALL
SELECT cars.brand, cars.id FROM cars WHERE cars.brand = 24 LIMIT 1
UNION ALL
SELECT cars.brand, cars.id FROM cars WHERE cars.brand = 29 LIMIT 1;

Don't want to miss updates? Please click the below button!

Follow MindStick