Hareshsinh's avatar

Mysql > get cars and cars-stocks data for specific date but if cars-stocks is not exist on specific date then show only cars data with null stocks

I have created a system where I have two tables namely cars and car stocks. Cars table stores the list of cars that the dealer sells and Car stocks table stores the no. of cars for each car that has at least one piece in stock. Please suggest a SQL query which gives two rows namely 'Car name' and 'No. of cars in stock'. car_name and car_stock_qty For a car that has zero stock , Null should be written on the second column .

I have tried below query , which works fine for smaller data , but takes too much time for larger data .

Query :- SELECT cars.id, cars.name, car_stocks.created_at FROM carsLEFT JOIN car_stocks ON car_stocks.car_id = cars.id and date(car_stocks.created_at) = '2022-01-18';

Please suggest if you have a more optimized/less time consuming query

0 likes
4 replies
Tray2's avatar

Can you show us the table structures and an example of the desired result.

Tray2's avatar

@Hareshsinh Copy and paste the code here wrapped wit three back ticks `

// Code goes here
1 like
Hareshsinh's avatar

@Tray2 Okay done it, please check the image link and let me know your feedback

Please or to participate in this conversation.