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';
Related
I have two tables, one with forecast production weights and one with actual production weight.
A customer can and will have multiple types and multiple seasons, and will also invoice those types over the year
Table Estimates
Customer Type Weight Season
John A 10 2018
John A 20 2018
John B 10 2018
Bill A 10 2018
Bill C 10 2017
Robert B 30 2017
Robert C 10 2018
Table Actual
Customer Type Weight InvoiceDate
John A 5 2018-10-30
John A 5 2018-10-30
John A 5 2018-10-30
John C 10 2018-10-30
Bill A 5 2018-11-1
Bill C 10 2017-11-30
Bill C 10 2017-11-30
Bill C 10 2017-11-30
Robert B 30 2017-11-10
Robert C 10 2019-2-20
Desired Query Would be as follows
select customer,
type,
sum(weight),
sum(weight)
from
estimates,
actual
where
season = 2018 and
InvoiceDate between 2018-7-1 and 2019-6-30 and
estimates.type = actual.type and
estimates.customer = actual.customer
group by
customer,
type
This give wildly large numbers
Desired result would be selecting for 2018
Customer Type Sum(Estimate) Sum(Actual)
John A 30 15
John B 10 0
John C 0 10
Bill A 10 5
Robert C 10 10
I have tried several join and union queries attempting to solve this issue
I cant quite get my head around which join to use to get the desired result
You can try below way -
select A.customer,A.type, estimated,actual
from
(
select customer,
type,sum(wieght) as estimated
from estimate where season=2018 group by customer,type
)A inner join
(
select customer,
type,sum(wieght) as actual
from actual where InvoiceDate between '2018-7-1' and '2019-6-30' group by customer,type
)B on A.customer=B.customer and A.type=B.type
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
Here's what I have and here's what I'm trying to accomplish:
teams TABLE
TeamID Season Coach1 Coach2
1 2011 35 22
2 2011 27
3 2012 11
4 2013 22 13
staff TABLE
StaffID Nickname
11 Bob
13 Rick
22 Mary
27 Steve
35 Joe
desired OUTPUT:
TeamID Season c1 c2
1 2011 Joe Mary
2 2011 Steve
3 2012 Bob
4 2013 Mary Rick
Here's my current MYSQL query:
SELECT gt.TeamID, gt.Season, gt.Coach1, c1.Nickname c1Nickname, gt.Coach2, c2.Nickname c2Nickname
FROM gladiator_teams as gt, staff as c1, staff as c2
WHERE gt.Coach1=c1.StaffID AND gt.Coach2=c2.StaffID
This returns:
TeamID Season c1 c2
1 2011 Joe Mary
4 2013 Mary Rick
I can't figure out how to modify the query to return rows that have NULL values for Coach 2. I suppose I need an IFNULL function to go somewhere, but I can't figure out where exactly. Any help is greatly appreciated!
You should use left JOIN
SELECT
gt.TeamID
, gt.Season
, gt.Coach1
, c1.Nickname c1Nickname
, gt.Coach2
, c2.Nickname c2Nickname
FROM gladiator_teams as gt
LEFT JOIN staff as c1 gt.Coach1=c1.StaffID
LEFT JOIN staff as c2 gt.Coach2=c2.StaffID
I have two tables: personal and persoLog. One represents personal and the other represents the log of personal events. The persoLog has a personal_log pointing to the personal table. I use join for this but either it is not working as expected or I know it wrong how to use it. Here are the tables:
ID NAME SECTION
1 joe driver
2 george technic
ID PERSONAL_ID EVENT DATE
1 1 idle November, 26 2014 12:28:52+0000
2 1 drive November, 26 2014 12:28:52+0000
3 1 idle November, 26 2014 12:28:52+0000
4 2 idle November, 26 2014 12:28:52+0000
5 2 idle November, 26 2014 12:28:52+0000
So I want to list the personal and their latest log like here:
ID NAME SECTION EVENT DATE
1 joe driver idle November, 26 2014 12:28:52+0000
2 george technic idle November, 26 2014 12:28:52+0000
I use this query
SELECT p.*, pl.event, pl.date
FROM personal p
RIGHT JOIN persoLog pl
ON p.id = pl.personal_id
I expected to return all personal with the only matching log but I got this instead:
ID NAME SECTION EVENT DATE
1 joe driver idle November, 26 2014 12:40:24+0000
1 joe driver drive November, 26 2014 12:40:24+0000
1 joe driver idle November, 26 2014 12:40:24+0000
2 george technic idle November, 26 2014 12:40:24+0000
2 george technic idle November, 26 2014 12:40:24+0000
What am I doing wrong?
That is the SQLfiddle link for my question:
http://sqlfiddle.com/#!2/da315a/2
Try this:
SELECT p.*,
pl.event, pl.date
FROM personal p
JOIN (
SELECT personal_id,max(date) as LastDate from persoLog
GROUP BY personal_id) x on p.id=x.personal_id
JOIN persoLog pl ON p.id = pl.personal_id
AND pl.date=x.lastDate
However, in your example data, all of the dates are the same in the log table. Fix that and the above should work
I believe this should work:
SELECT p.*, pl.event, pl.date
FROM personal p
LEFT JOIN persoLog pl ON p.id = pl.personal_id
GROUP BY p.id
ORDER BY date DESC
I'm struggling with a MySQL statement and was hoping for some guidance as I am close, but not quite there. I have a database that contains a table of property addresses and of property rental listings. The property addresses are related to a table or regions, which is related to a table of districts, which is then related to a table of suburbs.
I am trying to create a result which gives me the average rent in each suburb per month and by the number of bedrooms.
For example:
District Suburb Month YEAR YMD Bedrooms DataAverage
Nelson The Brook 01 2012 2012-01-01 00:00 1 190
Nelson The Brook 01 2012 2012-01-01 00:00 2 274
Nelson The Brook 01 2012 2012-01-01 00:00 3 341
Which I can then convert into a table as follows:
Average Rent
Beds by Suburb Jan-12 Feb-12 Mar-12 Apr-12 May-12 Jun-12 Jul-12
The Brook
1 $150 $245 $160 $285 $135 $370 $350
2 $330 $340 $380 $310 $335 $345 $355
3 $350 $380 $310 $395 $380 $350 $350
Inner City
1 $160 $245 $260 $285 $295 $300 $350
2 $360 $440 $480 $410 $535 $545 $555
3 $370 $480 $510 $595 $480 $450 $550
My Current SQL query is this:
SELECT d.name as District, s.name AS Suburb,
FROM_UNIXTIME(l.StartDate,'%m') AS Month,
FROM_UNIXTIME(l.StartDate,'%Y') AS YEAR,
FROM_UNIXTIME(l.StartDate, '%Y-%m-01 00:00') AS YMD,
p.Bedrooms,
REPLACE(FORMAT(AVG(l.RentPerWeek),0),',','') AS DataAverage
FROM properties p
LEFT JOIN listings l on l.property_id=p.id
LEFT JOIN regions r on p.region_id=r.id
LEFT JOIN districts d on d.region_id=r.id
LEFT JOIN suburbs s on s.district_id=d.id
WHERE FROM_UNIXTIME(l.StartDate) BETWEEN DATE(NOW()) - INTERVAL (DAY(NOW()) - 1) DAY - INTERVAL 11 MONTH AND NOW()
GROUP BY District, Suburb, Year, Month, Bedrooms
ORDER BY District, Suburb ASC, YMD ASC, Bedrooms ASC
Unfortunately what I am getting is the same result for each and every suburb. I think I may need to create a subquery SQL statement to get this to work properly, but I'm not entirely sure.
So I am getting something like this:
District Suburb Month YEAR YMD Bedrooms DataAverage
Nelson The Brook 01 2012 2012-01-01 00:00 1 190
Nelson The Brook 01 2012 2012-01-01 00:00 2 330
Nelson The Brook 01 2012 2012-01-01 00:00 3 350
Nelson The Brook 02 2012 2012-02-01 00:00 1 245
Nelson The Brook 02 2012 2012-02-01 00:00 2 340
Nelson The Brook 02 2012 2012-02-01 00:00 3 380
...
Nelson Inner City 01 2012 2012-01-01 00:00 1 190
Nelson Inner City 01 2012 2012-01-01 00:00 2 330
Nelson Inner City 01 2012 2012-01-01 00:00 3 350
Nelson Inner City 02 2012 2012-02-01 00:00 1 245
Nelson Inner City 02 2012 2012-02-01 00:00 2 340
Nelson Inner City 02 2012 2012-02-01 00:00 3 380
.etc.
Average Rent
Beds by Suburb Jan-12 Feb-12 Mar-12 Apr-12 May-12 Jun-12 Jul-12
The Brook
1 $150 $245 $160 $285 $135 $370 $350
2 $330 $340 $380 $310 $335 $345 $355
3 $350 $380 $310 $395 $380 $350 $350
Inner City
1 $150 $245 $160 $285 $135 $370 $350
2 $330 $340 $380 $310 $335 $345 $355
3 $350 $380 $310 $395 $380 $350 $350
Any pointers or assistance would be greatly appreciated.
Assuming that id is the primary key of each table, then according to your query text, a property is associated with a region, by virtue of the region_id column on the properties table:
FROM properties p
LEFT
JOIN regions r
ON p.region_id=r.id
A district is associated with a region (presumably, a district is a subdivision of a region.)
LEFT
JOIN districts d
ON d.region_id=r.id
and a suburb is associated with a district (presumably, a suburb is a subdivision of a district.)
LEFT
JOIN suburbs s
ON s.district_id=d.id
The net result is that every property within a region is getting associated with EVERY district within that region, and associated with EVERY suburb within each district.
So, you are getting the rent values averaged for all properties within a region.
To get rent values per suburb, you really need the relationship between a property and its suburb.
What you really need is a suburb_id column on the properties table as a foreign key to the suburbs table.
LEFT
JOIN suburbs s
ON s.district_id=d.id
AND s.id = p.suburb_id