I have a table named person(ID,fname,Age,Gender,parentID),
can anyone tell how to show persons who do not have grand childrens in SQL?
This should do the trick, but might be a bit slow if you have a lot of people with a lot of children:
SELECT DISTINCT p.*
FROM person p
LEFT JOIN person c ON c.parentID = p.ID
LEFT JOIN person gc on gc.parentID = c.ID
WHERE gc.ID IS NULL
SELECT *
FROM person
WHERE
ID NOT IN (
SELECT id
FROM (
SELECT p.*,c.id as childID,g.id AS grandChildId
FROM person p
LEFT JOIN person c
ON c.parentID = p.ID
LEFT JOIN person g
ON g.parentID = c.ID
) AS person_relation
WHERE grandChildId
is NOT NULL
Creating temporary tables can be helpful but for a single query this will work .
Related
I have a table with persons (personid,name), another table with camps (campid,title) and a third table with camp participations (id,personid,campid).
Now, for a given camp, I need a list of all other camp participations by all persons participating in the current camp.
But I have no idea how to join these tables. I have looked at a lot of other examples, but I can't really seem to get any inspiration from those...
This should work:
SELECT *
FROM PERSONS AS D INNER JOIN CAMP_PARTICIPATIONS AS E ON D.PERSONID = E.PERSONID
INNER JOIN CAMPS AS F ON F.CAMPID = E.CAMPID
WHERE F.CAMPID <> [your_camp] AND A.PERSONID IN (
SELECT A.PERSONID
FROM PERSONS AS A INNER JOIN CAMP_PARTICIPATIONS AS B ON A.PERSONID = B.PERSONID
INNER JOIN CAMPS AS C ON C.CAMPID = B.CAMPID
WHERE C.CAMPID = [your_camp] )
select persons.*,participations.*,camps.* from persons left join participations
on participations.personid=persons.personid
left join camps on camps.campid=participations.campid
where camp.campid=1;
now u can change the campid in where clause and place column name which u want in select clause
Using MySQL I am trying to pull ALL people who's company_id matches 3 and also compare whether that person exists in the favourites table.
What I have noticed is that if a person has no job_id then they are not output by the below query.
The below query outputs 1 row, but in the people table there are 3 rows that match the company_id of 3
SELECT *,
j.company_id as companyid,
f.id IS NOT NULL AS jid,
p.id as pid,
f.id AS fave_id,
f.id IS NOT NULL AS fave
FROM people p
INNER JOIN job j
ON p.job_id = j.id
LEFT JOIN favourites f
ON f.people_id=p.id
AND f.user_id = 12
WHERE p.company_id = 3
ORDER BY p.id ASC
SQLFiddle
Any help is appreciated to get the query to output all rows that match and also check the favourites table.
If you put a left join on job, it will show people that have no job_id. I have updated the fiddle here.
SELECT
p.*,
j.company_id as companyid,
f.id IS NOT NULL AS jid,
p.id as pid,
f.id AS fave_id,
f.id IS NOT NULL AS fave
FROM people p
LEFT JOIN job j
ON j.id = p.job_id
LEFT JOIN favourites f
ON f.people_id=p.id
AND f.user_id = 12
WHERE p.company_id = 3
ORDER BY p.id ASC
You should change your INNER JOIN for the job table to a LEFT OUTER JOIN so they're selected regardless of whether or not they exist in the job table.
I have three tables in Mysql that are link together:
Profile (ID, Name, Stuff..)
Contact(ID, ProfileID,desc,Ord)
Address(ID,ProfileID, desc, Ord)
Now I need to select all profile from the profile table, with the “desc” field from Contact and Address where Ord = 1. (this is for a search function where in a table I’ll display the name, main contact info and main Address of a client.
I can currently do this with three separate SQL request:
SELECT Name, ID FROM Profile WHERE name=”bla”
Then in a foreach loop, I’ll run the other two requests:
SELECT ProfileID, desc FROM Contact WHERE ProfileID=MyProfileID AND Ord=1
SELECT ProfileID, desc FROM Address WHERE ProfileID=MyProfileID AND Ord=1
I know you can do multiple SELECT in one query, is there a way I could group all three SELECT into one query?
You should be able to JOIN the tables on the profile.id and the profileid in the other tables.
If you are sure the profileid exists in all three tables, then you can use an INNER JOIN. The INNER JOIN returns matching rows in all of the tables:
select p.id,
p.name,
c.desc ContactDesc,
a.desc AddressDesc
from profile p
inner join contact c
on p.id = c.profileid
inner join address a
on p.id = a.profileid
where p.name = 'bla'
and c.ord = 1
and a.ord = 1
If you are not sure that you will have matching rows, then you can use a LEFT JOIN:
select p.id,
p.name,
c.desc ContactDesc,
a.desc AddressDesc
from profile p
left join contact c
on p.id = c.profileid
and c.ord = 1
left join address a
on p.id = a.profileid
and a.ord = 1
where p.name = 'bla'
If you need help learning JOIN syntax, here is a great visual explanation of joins
This query below only selects column when an ID from Profile table has atleast one match on tables: Contact and Address. If one or both of them are nullable, use LEFT JOIN instead of INNER JOIN because LEFT JOIN displays all records from the Left-hand side table regardless if it has a match on other tables or not.
SELECT a.*,
b.desc as BDESC,
c.desc as CDESC
FROM Profile a
INNER JOIN Contact b
ON a.ID = b.ProfileID
INNER JOIN Address c
ON a.ID = c.ProfileID
WHERE b.ORD = 1 AND
c.ORD = 1 AND
a.Name = 'nameHERE'
The LEFT JOIN version:
SELECT a.*,
b.desc as BDESC,
c.desc as CDESC
FROM Profile a
INNER JOIN Contact b
ON a.ID = b.ProfileID AND b.ORD = 1
INNER JOIN Address c
ON a.ID = c.ProfileID AND c.ORD = 1
WHERE a.Name = 'nameHERE'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
i have created working demo as your requirement :
The query bellow will retrieve all matching records from the database.its retrieving profile id,name stufff and description of contact tables
select p.id,p.name,p.stauff,c.descr,a.descr from profile as p
inner join contact as c on c.profileid=p.id
inner join address as a on a.profileid=p.id
where p.name="bla" and c.ord=1 and a.ord=1
I have a fairly complicated SQL statement I am working on. Here is where I am at:
SELECT c.category
FROM master_cat as c
LEFT JOIN
(
SELECT cat_id, user_id COUNT(cat_id) favoriteCat
FROM ratings
GROUP BY user_id
) a ON a.cat_id= c.cat_id
LEFT JOIN users AS u
ON u.user_id AND a.user_id
WHERE u.username = '{$user}' LIMIT 1
This statement is incomplete. I am missing a middle table here. cat_id is not actually in ratings. But items_id is from a table called items and cat_id is also in that table as well.
So what I am trying to do is this:
SELECT rating FROM ??? GROUP BY cat_id where u.user=$user
The only thing I can think of doing maybe is another LEFT join with items inside favoriteCat but I am not sure if that is allowed.
I was overthinking this, here is my final solution:
SELECT c.category, count(r.rating) AS totalCount
FROM ratings as r
LEFT JOIN items AS i
ON i.items_id = r.item_id
LEFT JOIN users AS u
ON u.user_id = r.user_id
LEFT JOIN master_cat AS c
ON c.cat_id = i.cat_id
WHERE r.user_id = '{$user_id}'
GROUP BY c.category
ORDER BY totalCount DESC
What I want is the Participants (people, with role as participant) of one event, combined with people of the same companies as that of participants, but whose role is manager.
A Person's role is stores in person_role_membership, but a person's company is stored in People. I have crafted the following query, which I think does the job pretty well. Please ignore the extra joins and information. I am fetching all the participants, then UNION-ing with all the managers, for which I am once again fetching all the participants. Now the trouble is this whole query, for a small subset is taking 9 seconds. Is there a way to make it faster?
select distinct *
from
(
SELECT
p.id as p_id, p.first_name as p_first_name, p.last_name as p_last_name,p_r.name as p_role,
p.job_title as p_job_title,
p_d.email as p_email, p_d.phone_1 as p_phone, p_d.phone_ext_1 as p_ext,
c.name as p_company, p_c.name as p_parent_company
FROM person_role_memberships as prm
left join people as p on prm.person_id = p.id and prm.person_role_id between 32 and 35
left join person_roles as p_r on p_r.id = prm.person_role_id
left join person_details as p_d on p.id = p_d.person_id and p_d.type = 'BusinessDetail'
left join companies as c on c.id = p.company_id
left join companies as p_c on p_c.id = p.parent_company_id
where
p.id is not null
) as parts
union (
select p.id, p.first_name,p.last_name,prm.person_role_id, p.job_title, 'cp email', 'phone','ext',c.name, d.name
from people as p -- All those people
left join person_role_memberships as prm on prm.person_id = p.id -- whose roles are like this
left join companies as c on p.company_id = c.id
left join companies as d on c.parent_id = d.id
-- and whose companies are like those people whose roles are like this
where company_id = any
(
select company_id from people as p
left join person_role_memberships as prm on prm.person_id = p.id
where prm.person_role_id between 32 and 35-- and other conditions;
)
and (person_role_id = 14 or person_role_id = 15))
My guess would be that the 'any' part of your query is slowing things down. By the looks of it I think you don't even need it because you could filter on person_role_memberships directly:
select p.id, p.first_name,p.last_name,prm.person_role_id, p.job_title, 'cp email', 'phone','ext',c.name, d.name
from people as p -- All those people
left join person_role_memberships as prm on prm.person_id = p.id -- whose roles are like this
left join companies as c on p.company_id = c.id
left join companies as d on c.parent_id = d.id
-- and whose companies are like those people whose roles are like this
where prm.person_role_id between 32 and 35 -- and other conditions;
and (person_role_id = 14 or person_role_id = 15))
The 'any'-statement is causing the database to look through the entire PEOPLE table for every record.