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;
Related
I am not so into DB and I have the following problem working on a MySql query that need an IF condition into the WHERE statment.
I need this IF condition because the where statment have to be different in base to a country id retrieved by the query itself. I try to explain you what I need in details.
This is my query (that is not working):
SELECT
LCZ1.id AS localization_id,
LCZ1.description AS localization_description,
CNT.id AS country_id,
CNT.country_name AS country_name,
CNT.isActive AS country_is_active,
RGN.id AS region_id,
RGN.region_name AS region_name,
PRV.id AS province_id,
PRV.province_name AS province_name,
DST.id AS district_id,
DST.district_name AS district_name,
SCT.id AS sector_id,
SCT.sector_name AS sector_name
FROM Localization AS LCZ1
LEFT JOIN Country AS CNT
ON LCZ1.country_id = CNT.id
LEFT JOIN Region AS RGN
ON LCZ1.region_id = RGN.id
LEFT JOIN Province AS PRV
ON LCZ1.province_id = PRV.id
LEFT JOIN District AS DST
ON LCZ1.district_id = DST.id
LEFT JOIN Sector AS SCT
ON LCZ1.sector_id = SCT.id
WHERE
(LCZ1.country_id = (SELECT LCZ2.country_id FROM Localization AS LCZ2 WHERE LCZ2.id = 5))
IF(LCZ1.country_id = 1)
BEGIN
AND
LCZ1.country_id is not null
END
IF(LCZ1.country_id = 2)
BEGIN
AND
LCZ1.country_id is not null
END
As you can see the WHERE condition start with this:
WHERE
(LCZ1.country_id = (SELECT LCZ2.country_id FROM Localization AS LCZ2 WHERE LCZ2.id = 5))
basically I am selecting the LCZ1.country_id value performing a second select query that works by localizations_id. I am specifying a localization retrieving its country_id that is used as value of this where condition. It works fine.
Then I need to use this retrieved value to add some AND clause to my WHERE condition. The country_id can be only 1 or 2.
So,
if the retrieved country_id value is 1 --> add an AND condition.
if the retrieved country_id value is 2 --> add another ADD condition.
How can I fix my query and correctly handle this situation?
You could use an inner join with the the proper ON and AND clause avoiding where
SELECT ......
FROM Localization AS LCZ1
LEFT JOIN Country AS CNT
ON LCZ1.country_id = CNT.id
LEFT JOIN Region AS RGN
ON LCZ1.region_id = RGN.id
LEFT JOIN Province AS PRV
ON LCZ1.province_id = PRV.id
LEFT JOIN District AS DST
ON LCZ1.district_id = DST.id
LEFT JOIN Sector AS SCT
ON LCZ1.sector_id = SCT.id
INNER Localization LCZ2 ON LCZ1.country_id = LCZ2.country_id
AND LCZ2.id = 5 AND LCZ1.country_id = 1 AND LCZ1.country_id is not null
You don't need the IF statements, you just need to rethink your WHERE clause a little.
Since you are wanting to add the LCZ1.country_id is not null predicate whenever LCZ1.country_id is 1 or 2, you could reformulate that to simply check that LCZ1.country_id is one of the required values:
AND LCZ1.country_id in (1, 2)
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
)
Got 2 tables as shown above. I want to update 'leaverecord.Consumed' from 'approved.Consumed' WHERE the leaverecord.name = approved.name and leaverecord.leavetype = approved.leavetype. Tried below query by getting error of 'Invalid use of group function
UPDATE leaverecord r INNER JOIN approved a
ON r.name = a.name
SET r.Consumed = SUM(DATEDIFF(a.todate,a.fromdate))
WHERE r.leavetype = a.leavetype AND
r.name = a.name
Calculate the consumed in a subquery and INNER JOIN it with leaverecord and then do the UPDATE like below. No WHERE clause needed.
Try this:
update leaverecord r
inner join (
select name, leavetype, sum(datediff(todate,fromdate)) consumed
from approved
group by name, leavetype
) a on r.name = a.name
and r.leavetype = a.leavetype
set r.consumed = a.consumed;
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
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'