user_car
car_id model_year brand_id
1 2006 1
2 2010 3
3 2015 2
4 2015 1
5 2015 1
And
car_brand
brand_id name
1 BMW
2 MAZADA
3 JEEP
If I search "BMW2015" OR "2015BMW" OR "2015 BMW"
I can't get the result in above sql query, how can i search in proper way?
car_id brand_id model_year name
4 1 2015 BMW
5 1 2015 BMW
If I search "BMW2015" OR "2015BMW" means you will be getting no rows only, if you search "BMW 2015" or "2015 BMW" means you manually split with " " and try the below query.
select * from user_car u inner join car_brand c on u.brand_id=c.brand_id where model_year like "%2015%" or model_year like "%BMW%" or name like "%2015%" or or name like "%BMW%" group by car_id order by car_id
Related
Lets say we have this table
id brand date
1 audi ....
2 toyota .....
3 toyota ....
4 audi ....
5 audi .....
when i use group by on brand(select * from table group by brand) it returns first occurances which is
id brand date
1 audi ....
2 toyota .....
Is it possible to get last occurances with GROUP BY clause. Mwith last occurance i mean latest id and date just like
id brand date
5 audi ....
3 toyota .....
select brand, max(date) from table group by brand;
I've the following product table in my database. I want to get the rows of all the products with distinct product_name added in the month of Dec in the descending order of their id.
id product_name qty month
1 Apple 20 Dec
2 Banana 40 Jan
3 Cherry 60 Jun
4 Apple 25 Dec
5 Banana 50 Dec
6 Papaya 20 Dec
7 Guava 34 Aug
8 Watermelon 55 Mar
9 Apple 75 Dec
10 Orange 32 Dec
The resulting table should be as below:
id product_name qty month
10 Orange 32 Dec
9 Apple 75 Dec
6 Papaya 20 Dec
5 Banana 50 Dec
So far I've come up with the following query and it doesn't get the result I need.
SELECT *
FROM product
WHERE month = "Dec"
GROUP BY product_name
ORDER BY id DESC
The problem with this query is that it gets the first row/record of Apple rather than last one as shown in table below:
id product_name qty month
10 Orange 32 Dec
6 Papaya 20 Dec
5 Banana 50 Dec
1 Apple 20 Dec
Can anyone help me write a proper mysql query for this?
You can use the following query:
SELECT MAX(id), product_name
FROM product
WHERE month = "Dec"
GROUP BY product_name
to get the biggest id per product_name:
Output:
max_id product_name
9 Apple
5 Banana
10 Orange
6 Papaya
Using the above query as a derived table you can join back to the original table in order to get the rest of the fields:
SELECT t1.*
FROM product AS t1
INNER JOIN (
SELECT MAX(id) AS max_id, product_name
FROM product
WHERE month = "Dec"
GROUP BY product_name
) AS t2 ON t1.id = t2.max_id and t1.product_name = t2.product_name
ORDER BY t1.id
Demo here
I'm trying to return a list of vehicles where I know they are not already booked for a current date range. I have a tblVehicles table:
ID Name
1 BMW
2 Merc
3 Land Rover
4 Hummer
5 Convertable
I also have a table (tblDiary) that holds all the dates that vehicle is being used:
ID VehicleId startDate endDate
1 1 2016-06-20 2016-06-22
2 1 2016-06-24 2016-06-24
3 2 2016-05-01 2016-06-05
So I'm trying to write a query where I want to find vehicles that are available between 2016-06-21 & 2016-06-23. So, in this instance the desired result would be:
ID Name
2 Merc
3 Land Rover
4 Hummer
5 Convertable
What the most efficient way of doing this?
SELECT *
FROM tblVehicles v
WHERE NOT EXISTS
(
SELECT NULL
FROM tblDiary d
WHERE d.vehicleId = v.id
AND endDate >= '2016-06-21'
AND startDate <= '2016-06-23'
)
I have a problem with sql query. I need join three tables, count rows, get last row, and the create custom column.
The example my tables like below:
table name: article
_______________________________________
idarticle idwriter title
---------------------------------------
1 1 Title One
2 3 Title Two
3 2 Title Three
table name: comment
________________________________________________________________________________
idcomment idarticle idcommented content datetime
--------------------------------------------------------------------------------
1 1 2 Comment One 2015-05-15 00:00:00
2 1 3 Comment Two 2015-05-16 00:00:00
3 1 1 Comment Three 2015-05-17 00:00:00
4 2 2 Comment Four 2015-05-18 00:00:00
5 3 3 Comment Five 2015-05-19 00:00:00
6 3 2 Comment Six 2015-05-20 00:00:00
table name: member
_____________________
idmember username
---------------------
1 apple
2 orange
3 banana
How to join all tables, count, and get last commented+username with one query.
May the result like:
_____________________________________________________________________________________________________________________
idarticle idwriter title username_writer totalcomments lastcomment_id lastcomment_username lastcomment_datetime
---------------------------------------------------------------------------------------------------------------------
3 2 Title Three orange 2 2 orange 2015-05-20 00:00:00
2 3 Title Two banana 1 2 orange 2015-05-18 00:00:00
1 1 Title One apple 3 1 apple 2015-05-17 00:00:00
I hope someone will solve my problem.
I using PHP 5.4, MySQL 5.5, and MeekroDB library, so the query must support with those.
Sorry for my English.
Thank you
http://sqlfiddle.com/#!9/132336/1
SELECT a.idarticle, a.idwriter , a.title,
m.username,
c.idcomment, cm.username, c.datetime
FROM article a
LEFT JOIN member m
ON a.idwriter = m.idmember
LEFT JOIN comment c
ON a.idarticle = c.idarticle
LEFT JOIN comment c1
ON a.idarticle = c1.idarticle
AND c.datetime<c1.datetime
LEFT JOIN member cm
ON c.idcommented = cm.idmember
WHERE c1.datetime IS NULL
I am new to this site and I need the help for following
person table:
driver_id name address
1 sankar karaikal
2 vivek chennai
3 kumaran pondy
4 siva chennai
car table
license model year
22 bmw 2014
23 toyata 2014
24 audi 2015
25 maruti 2014
owns table
driver_id license
1 22
2 23
3 24
4 25
Question is how to find who owned cars that were involved in accident in 2014.
I need a sql query for this and thanks in advance.
select name from person p
inner join
owns o on
(p.driver_id=o.driver_id)
inner join
car c on (c.license=o.license)where year='2014';