MySql showed me no result with my query - mysql

I have 4 tables which are (model, license, pilot, and person)
Model: mid, name
License: mid, pid, licenseDate
Pilot: pid, hireDate
person: pid, firstName, lastName
I want to get the names of pilots who are not licensed to fly any airplane
I tried this query, but it showed me no result!!!
select model.mid, license.pid, license.mid, license.licenseDate, pilot.pid, pilot.hireDate, person.firstName, person.lastName
from model, license, pilot, person
where pilot.pid = license.pid and model.mid = license.mid and pilot.pid = person.pid
and not exists(SELECT null FROM model WHERE pilot.pid = license.pid);

try this... using inner join.
SELECT model.mid, license.pid, license.mid, license.licenseDate, pilot.pid, pilot.hireDate, person.firstName, person.lastName
FROM model
INNER JOIN license ON (model.mid = license.mid)
INNER JOIN pilot ON (pilot.pid = license.pid)
INNER JOIN person ON (pilot.pid = person.pid)
WHERE pilot.pid = license.pid AND model.mid = license.mid AND pilot.pid = person.pid
AND pilot.pid != license.pid);

Not tested the script, but You can fetch pilots which are not in License table means they're not licensed to fly a plane, then you can join the result with person table to fetch the particular name.
select p.firstname, p.lastname
from
(
select pid from pilot
where not exists (select pid from License where License.pid = pilot.pid)
) DT
inner join person p
on p.pid = DT.pid

Give this a try:
SELECT pe.firstName, pe.lastName FROM pilot p
LEFT JOIN license l ON p.pid = l.pid
JOIN person pe ON p.pid = pe.pid
WHERE l.pid IS NULL

Related

I want to Query w3school database

The database is at: http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all . Particulary I want to get the list of customers who placed product orders on July 1996 from Speedy Express Shipping Company and were served by employees: Davolio Nancy and Peacock Margaret. The result should have:
Name of customer, contact, address, city, postal code and country
The orderDetailID they placed
Contacts of the shipping company
Product name, price and quantity
Product name, price and units bought
Category of the products
Here is my query:
SELECT
c.CustomerName,
c.ContactName,
c.Address,
c.City,
c.PostalCode,
c.Country,
o.OrderDetailID,
s.Phone,
r.ProductName,
r.Price,
o.Quantity,
g.CategoryName
FROM Customers c
JOIN Orders d
on (c.CustomerID = d.CustomerID)
LEFT JOIN OrderDetails o
on (o.OrderID = d.OrderID)
LEFT JOIN Shippers s
on (s.ShipperID = d.ShipperID)
LEFT JOIN Products r
on (r.ProductID = o.ProductID)
LEFT JOIN Categories g
on (g.CategoryID = r.CategoryID)
LEFT JOIN Employees e
on (e.EmployeeID = d.EmployeeID)
WHERE s.ShipperName = 'Speedy Express' AND
((e.LastName = 'Davolio' AND e.FirstName = 'Nancy') OR
(e.lastName = 'Peacock' AND e.firstName = 'Margaret')) AND
EXTRACT(YEAR_MONTH FROM d.OrderDate) = 199607;
I get error which says:
ERROR 1: Could not prepare statement (1 near "FROM": syntax error)
When I remove the last condition (of extracting the date) as follows:
SELECT c.CustomerName, c.ContactName, c.Address, c.City, c.PostalCode, c.Country, o.OrderDetailID, s.Phone, r.ProductName, r.Price, o.Quantity, g.CategoryName FROM Customers c JOIN Orders d on (c.CustomerID = d.CustomerID) LEFT JOIN OrderDetails o on (o.OrderID = d.OrderID) LEFT JOIN Shippers s on (s.ShipperID = d.ShipperID) LEFT JOIN Products r on (r.ProductID = o.ProductID) LEFT JOIN Categories g on (g.CategoryID = r.CategoryID) LEFT JOIN Employees e on (e.EmployeeID = d.EmployeeID) WHERE s.ShipperName = 'Speedy Express' AND((e.LastName = 'Davolio' AND e.FirstName = 'Nancy') OR (e.lastName = 'Peacock' AND e.firstName = 'Margaret'));
the error disappears. So please how can i fix the year_month condition without getting error!
One possibility here is that your version of MySQL, for whatever reason, does not support YEAR_MONTH being used with EXTRACT(). One workaround here would be to use DATE_FORMAT() instead:
WHERE DATE_FORMAT(d.OrderDate, '%Y%m') = '199607'
To test whether you have an old version of EXTRACT(), just try running the following simple query:
SELECT EXTRACT(YEAR_MONTH FROM NOW());
If this errors out, then my conjecture is correct.

Show names from two values

I am trying to get a SQL code but can't really figure out how to do it so I will explain what I want.
I have 4 tables called Person, Customer, Adres and Store. Now I have to show each customer NAMES which lives in the same city as where there is a Store. So First I figured out which persons are customers by:
SELECT person_name
FROM person
WHERE person_id IN
(SELECT Person_Person_Id
FROM customer);
Which stores are in which city:
SELECT Store_name, adres_city
FROM store s, adres a
WHERE s.Adres_Adres_Id = a.adres_id;
Note that person_person_id is the same as person_id just as a fk.
I am stuck at this code and don''t know how to go further from here. My column name of table adres = adres_city.
Okay, if I realised what do you want, try to do this:
select --distinct
b.Adres_City,
a.person_id
from
dbo.Person a
join dbo.Adres b on a.adres_adres_id = b.Adres_Id
join dbo.Customer c on a.person_id = c.Person_Person_Id
join dbo.Store d on b.Adres_Id = d.Adres_Adres_Id
If you are not sure, that all your keys in tables are uniq, uncomment --distinct in the first string.
Or, if you are want to know statistics among your cities, do this:
select
b.Adres_City,
count(distinct a.person_id) as cnt
from
dbo.Person a
join dbo.Adres b on a.adres_adres_id = b.Adres_Id
join dbo.Customer c on a.person_id = c.Person_Person_Id
join dbo.Store d on b.Adres_Id = d.Adres_Adres_Id
group by b.Adres_City
Please let me know, if it will help you.
Update1:
select --distinct
b.Adres_City,
a.person_id
from
dbo.Person a
join dbo.Adres b on a.adres_adres_id = b.Adres_Id
join dbo.Customer c on a.person_id = c.Person_Person_Id
where
b.Adres_City in (
select y.Adres_City
from dbo.Store x join dbo.Adres y on y.Adres_Id = x.Adres_Adres_Id
)

Multiple Join Searching in Database

I want to fetch the records from database in my ongoing project.
I have 5 Tables; Everyone make profile which is saved in starting four tables then define partner profile. I want to to fetch all the partner name which meets profile.
This is my tables structure:
Basic Info (reg_id, name, gender, dob, martial_status)
Education (id,reg_id,education,college)
Location (id,reg_id,country,state, city)
Other_details (id,reg_id,height, weight)
Partner (id, reg_id, gender, dob, education, college, country, state, city, height, weight,martial_status) [ This is the Main Table]..
So far I tried this but no luck:
SELECT `basic_info`.`reg_id`,
`basic_information`.`dob`,
`other_detail`.`height`,
`location`.`city`,
`education`.`education`
FROM `basic_information`
INNER JOIN(
SELECT *
FROM `patner`
WHERE `patner`.`reg_id`='shi01') `patner`
ON `basic_information`.`martial_status`=`partner`.`martial_status`
AND `basic_information`.`reg_id`!='shi01'
INNER JOIN `education`
ON `patner`.`education`=`education`.`education`
AND `patner`.`education`=`education`.`education`
INNER JOIN `location`
ON `patner`.`city`=`location`.`city`
INNER JOIN `other_detail`
ON `patner`.`bodytype`=`other_detail`.`body_type`
AND `patner`.`skin`=`other_detail`.`skin`
AND `patner`.`height1` <= `other_detail`.`height`
GROUP BY `basic_information`.`reg_id`;
You did some mistakes in writing join conditions. I have tried to fix it. Try this, It should work;
SELECT BI.reg_id, BI.name
FROM basic_info BI
INNER JOIN
Education E ON BI.reg_id = E.reg_id
INNER JOIN
Location L ON BI.reg_id = L.reg_id
INNER JOIN
Other_details OD ON BI.reg_id = OD.reg_id
INNER JOIN
(Select reg_id, martial_status, height, education, city FROM Partner where reg_id = 'Your_ID') P
ON BI.reg_id = P.reg_id
WHERE
BI.marital_status = P.marital_Status
AND E.Education = P.Education
AND L.city = P.City
AND OD.height <= P.Height
AND BI.reg_id <> 'Your_ID';

MySql: How to check field has data in MySql?

Here is my code and result :
SELECT DISTINCT
CAL.CarListingId,
(SELECT
IF(REPLACE(carImage.ImageUrl,'~','') IS NULL,'asdf',REPLACE(carImage.ImageUrl,'~',''))
FROM
carImage
WHERE
IsMainImage = 1 AND Status = 1
AND CarListingId = CAL.CarListingId) AS ImageUrl,
CAL.ListingNumber,
CAL.Caption,
CAL.Year,
CAL.Km,
CAL.Color,
CAL.Price,
CONCAT((SELECT Name FROM City WHERE CityId IN (SELECT CityId FROM County WHERE CountyId = CAL.CountyId)),'/', (SELECT Name FROM County WHERE CountyId = CAL.CountyId)) AS Region,
CAL.Creation
FROM
carlisting AS CAL
INNER JOIN
User AS U ON U.UserId = CAL.CreatedBy
INNER JOIN
carlistingcategory AS CLC ON CLC.CarListingId = CAL.CarListingId
LEFT JOIN CarImage AS CI ON CI.CarListingId = CAL.CarListingId
ORDER BY CAL.Creation;
I use this query as a subquery in another query. I need to check this query's result if it is `NULL`. But as you can see there is no data so `IS NULL` returns false. How can I check the sub query has data ?
try this query:
SELECT DISTINCT
CAL.CarListingId,
CAL.ListingNumber,
CAL.Caption,
CAL.Year,
CAL.Km,
CAL.Color,
CAL.Price,
CONCAT((SELECT Name FROM City WHERE CityId IN (SELECT CityId FROM County WHERE CountyId = CAL.CountyId)),'/', (SELECT Name FROM County WHERE CountyId = CAL.CountyId)) AS Region,
CAL.Creation,
( case when CI.ImageUrl IS NULL then 'asdf' else CI.ImageUrl
end)
FROM
carlisting AS CAL
LEFT JOIN CarImage AS CI ON CI.CarListingId = CAL.CarListingId
INNER JOIN User AS U ON U.UserId = CAL.CreatedBy
INNER JOIN carlistingcategory AS CLC ON CLC.CarListingId = CAL.CarListingId
ORDER BY CAL.Creation;

How do I do a partial match on an IN statement in MySQL

I am joining multiple tables into a single query. I need to do a partial match on values in an IN statement. Here is an example.
SELECT DISTINCT
am.id AS id,
am.flagged AS flagged,
am.name AS name,
am.type AS type,
am.file AS file,
am.s3_tag AS s3_tag,
am.low_s3_tag AS low_s3_tag
FROM accounts_media am
LEFT JOIN accounts_location_media alm ON am.id = alm.media_id
LEFT JOIN accounts_location al ON al.id = alm.location_id
LEFT JOIN accounts_person_media apm ON am.id = apm.media_id
LEFT JOIN accounts_person ap ON ap.id = apm.person_id
LEFT JOIN accounts_event_media_record aemr ON am.id=aemr.media_id
LEFT JOIN accounts_medianote_media_record amma ON am.id=amma.media_id
LEFT JOIN accounts_medianote amn ON amma.medianote_id=amn.id
WHERE
am.account_id = '1234'
AND am.flagged = FALSE
AND ('Da' IN (SELECT first_name FROM accounts_person WHERE account_id = '1234')
AND ('Rob' IN (SELECT first_name FROM accounts_person WHERE account_id = '1234')
In the
AND ('Da' IN (SELECT first_name FROM accounts_person WHERE account_id = '1234')
statement there are values that say 'Dan', 'Daniel', etc. in the table. There is also 'Rob' and 'Robert'. I need that statement to make sure and name that contains 'Da' AND any name that contains 'Rob' from that table. Is there a way to do this?
So a record can be linked to multiple people in the accounts_person table. So lets say I have three records.
Record One: A person named Dan is attached to the record.
Record Two: A person named Robert is attached to the record.
Record Three: A person named Dan and a person named Robert are attached to the record.
I want the query to only return Record Three because it has the match of 'Da' and 'Rob'.
Try this:
WHERE
am.account_id = '1234'
AND am.flagged = FALSE
-- AND ( ap.first_name LIKE '%Da%' OR ap.first_name LIKE '%Rob%')
AND EXISTS
( SELECT 1
FROM accounts_person apx
WHERE apx.first_name LIKE '%Da%'
AND apx.account_id = am.account_id
)
AND EXISTS
( SELECT 1
FROM accounts_person apy
WHERE apy.first_name LIKE '%Rob%'
AND apy.account_id = am.account_id
)
I'm not sure, but I think you want a like statement, with a wild card after the Da.
SELECT DISTINCT
am.id AS id,
am.flagged AS flagged,
am.name AS name,
am.type AS type,
am.file AS file,
am.s3_tag AS s3_tag,
am.low_s3_tag AS low_s3_tag
FROM accounts_media am
LEFT JOIN accounts_location_media alm ON am.id = alm.media_id
LEFT JOIN accounts_location al ON al.id = alm.location_id
LEFT JOIN accounts_person_media apm ON am.id = apm.media_id
LEFT JOIN accounts_person ap ON ap.id = apm.person_id
LEFT JOIN accounts_event_media_record aemr ON am.id=aemr.media_id
LEFT JOIN accounts_medianote_media_record amma ON am.id=amma.media_id
LEFT JOIN accounts_medianote amn ON amma.medianote_id=amn.id
WHERE
am.account_id = '1234'
AND am.flagged = FALSE
AND (accounts_person.first_name like '%Da%'
OR accounts_person.first_name like '%Rob%')
AND accounts_person.account_id = '1234'