SELECT
People.Name, People.Secondname
CONCAT(People.Name," ", People.Secondname)
FROM People, Shop, Circus
WHERE CONCAT(People.Name," ", People.Secondname) != Shop.Buyer
AND CONCAT(People.Name," ", People.Secondname) != Circus.Watcher
Ok, so this is my question. There are 2 columns. I have concat'ed them and want to find list of people who havent been in Shop AND Circus. Like: I have concated full name - "Jhon Jhonson". He havent been in circus and havent been in shop. So i want him to be shown.
Becouse I cant publish picture here, at least i can get link of table i am trying to make... http://imm.io/1k5tJ
I hope you can decipher what I want to say.
try
SELECT
People.Name, People.Secondname
CONCAT(People.Name," ", People.Secondname)
FROM People left outer join Shop on CONCAT(People.Name," ", People.Secondname) = Shop.Buyer
left outer join Circus on CONCAT(People.Name," ", People.Secondname) = Circus.Watcher
where Shop.Buyer is null and Circus.Watcher is null
Based on your comment above of: "i would like to find whom are in Concated column, but not in the Users AND not in Participants", then this query will let you choose all tbl1 data where the user AND participant do not exist in the secondary and tertiary tables respectively.
select
tbl1.name,
tbl1.name2
from People tbl1
left join Show1 tbl2 on tbl1.name = tbl2.user
left join Show2 tbl3 on tbl1.name2 = tbl3.participant
where tbl2.user is null
and tbl3.participant is null
I think your description of the problem is still unclear, but this is a step in the right direction.
Update: Change column names based on your comments on question.
Related
i have 4 tables in MYSQL and i want to show count of learner_history table's status column district wise for all 36 districts, however when i query it shows only 1 district result even i tried left join to show district names weather there is data available or not it must show null or zero in its position.Data should be like this for all districts:
database structure is like this
[enter image description here][i draw the relation of tables in this image]
please suggest what query would give results like this, thanks.
i draw the relation of tables in this image13.jpg
It was pretty awkward to work from with images, but it should get you what you are asking for. Also wasn't sure what you were wanting with 'Str.#' - the example image you gave with 22 as every result wasn't the most helpful.
select
District.District_Name as District,
sum(Learner_History.`Status` = 'catchment') as Catchment,
sum(Learner_History.`Status` = 'fresher') as Fresher,
sum(Learner_History.`Status` = 'dropout') as Dropout,
sum(Learner_History.`Status` = 'missout') as Missout,
sum(Learner_History.`Status` = 'mainstrain') as Mainstrain,
sum(Learner_History.`Status` = 'pec') as PEC
from District
left join School on School.District_ID = District.District_ID
left join Learner on Learner.School_ID = School.School_ID
left join Learner_History on Learner_History.Learner_ID = Learner.Learner_ID
group by District.District_Name;
I think this is an easy question but i can't find a answer I don't have much time left...
help me please
I have a Table "Anime" with the columns "animeID, Name, ..." and I have the Table "anime_user2" with the columns "anime_userID, ..., users_userID, anime_idAnime(Foreign Key)"
Now I want to get all Names from Table "Anime" that are not in already in the Table "anime_user2" with the ID from the user that is logged in
I tried this but it doesn't work as I wanted :
"SELECT Name FROM Anime LEFT JOIN anime_user2 ON idAnime = anime_idAnime WHERE users_userID = $userID AND idAnime != anime_idAnime"
There is probably an easier way to solve this but I can't see it?
You can do as
select
a.Name
from
Anime a
left join anime_user2 au
on au.anime_idAnime = a.animeID
where
au.anime_idAnime is NULL
Here is a demo how it works
How do you find out who did not contribute to a particular fund raiser that we all just did. There are many titles to the different charities, I however just want to extract the non-contributors for a particular charity title. Is there anyway to do this? When I do the the syntax below it comes up as an empty set. The search is done by way of the table Id matching and left joins. Please see below.
SELECT
moiid,
trim(concat(name.fname,' ' ,name.mname,' ',name.lname)) as Brother,
name.moiid as Members_ID,
sum(otherpay.othpayamt) as NO_Contribution,
quadlt.ltfname as quad
FROM name
LEFT JOIN OTHERPAY ON name.moiid = otherpay.othpaymoiid
LEFT JOIN quadlt ON name.quadlt = quadlt.ltid
WHERE Otherpay.othpaytitle like '%food drive%'
AND otherpay.othpaymoiid IS NULL
AND name.type = 'BOI'
AND name.type <> 'jrboi'
AND name.city = 'SUFFOLK'
GROUP BY brother
ORDER BY name.quadlt, brother
When you add conditions to the where clause for tables that are left joined, you effectively turn them into an inner join, requiring them to return records.
You can move the conditions to the join itself:
SELECT moiid, trim(concat(name.fname,' ' ,name.mname,' ',name.lname)) as Brother, name.moiid as Members_ID, sum(otherpay.othpayamt) as NO_Contribution, quadlt.ltfname as quad
FROM name
LEFT JOIN OTHERPAY
ON name.moiid = otherpay.othpaymoiid
AND Otherpay.othpaytitle like '%food drive%'
LEFT JOIN quadlt ON name.quadlt = quadlt.ltid
WHERE
otherpay.othpaymoiid IS NULL
AND name.type = 'BOI'
AND name.type <> 'jrboi'
AND name.city = 'SUFFOLK'
GROUP BY brother
ORDER BY name.quadlt, brother
I have a few tables which I have joined together and would like to join a table that has multiple columns. My current query is as follows:
select
usrs.firstname, usrs.middleNames, usrs.surname,
if (usrs.sex=0,'Male','Female') as sex,
usrs.DOB,
(YEAR(CURDATE())-YEAR(usrs.DOB)) - (RIGHT(CURDATE(),5)<RIGHT(usrs.DOB,5)) AS age,
birth.townName AS 'birthTown', birth.regionName AS 'birthRegion', birth.countryName AS 'birthCountry',
location.townName AS 'curTown', location.regionName AS 'curRegion', location.countryName AS 'curCountry',
usrs.email, emails.email AS 'alternateEmail',
numbers.number,
usrs.website,
usrs.aboutMe,
family.mother, family.father, family.partner, marital.status, family.aboutFamily,
children.name AS 'childsName'
from ch09.tbl_users usrs
LEFT JOIN vw_town_region_country birth ON birth.townID = usrs.birthPlace
LEFT JOIN vw_town_region_country location ON location.townID = usrs.currentLocation
LEFT JOIN tbl_alternate_emails emails ON emails.userID = usrs.id
LEFT JOIN tbl_contact_numbers numbers ON numbers.userID = usrs.id
LEFT JOIN tbl_family family ON family.userID = usrs.id
LEFT JOIN tbl_marital_status marital ON family.maritalStatusID = marital.id
LEFT JOIN tbl_children children ON family.id = children.familyID
I put my whole query it might be a bit wrong or cleaner way to do it. The issue is with the tbl_children, as it is "one to many" it results in multiple rows for a single user for every child that user has in the tbl_children table.
So my results are:
userID:1 firstName middleNames surname ....... childsName
userID:1 firstName middleNames surname ....... childsName
userID:1 firstName middleNames surname ....... childsName
I would prefer:
userID:1 firstName middleNames surname ....... childsName childsName2 childsName3
Is it possible to do this through a Join somehow? Obviously it isn't acceptable for me to have multiple entries per user on the view.
You could use the function GROUP_CONCAT in combination with GROUP BY for this. GROUP_CONCAT let's you aggregate values from a column by concatenating them. Note that this will not give you a column for every child, but one column with a string containing all the names.
EDIT; your query would become something like:
select
usrs.firstname, usrs.middleNames, usrs.surname,
if (usrs.sex=0,'Male','Female') as sex,
usrs.DOB, (YEAR(CURDATE())-YEAR(usrs.DOB)) - (RIGHT(CURDATE(),5)<RIGHT(usrs.DOB,5)) AS age,
birth.townName AS 'birthTown', birth.regionName AS 'birthRegion', birth.countryName AS 'birthCountry',
location.townName AS 'curTown', location.regionName AS 'curRegion', location.countryName AS 'curCountry',
usrs.email, emails.email AS 'alternateEmail',
numbers.number,
usrs.website,
usrs.aboutMe,
family.mother, family.father, family.partner, marital.status, family.aboutFamily,
GROUP_CONCAT(children.name SEPERATOR ",") AS 'childsName'
FROM ch09.tbl_users usrs
LEFT JOIN vw_town_region_country birth ON birth.townID = usrs.birthPlace
LEFT JOIN vw_town_region_country location ON location.townID = usrs.currentLocation
LEFT JOIN tbl_alternate_emails emails ON emails.userID = usrs.id
LEFT JOIN tbl_contact_numbers numbers ON numbers.userID = usrs.id
LEFT JOIN tbl_family family ON family.userID = usrs.id
LEFT JOIN tbl_marital_status marital ON family.maritalStatusID = marital.id
LEFT JOIN tbl_children children ON family.id = children.familyID
GROUP BY userID
Assuming that the number of children is unknown at the time of writing the query (i.e., a user could have 0 or 1 or 5 children), making a pivot like this probably isn't the best route for getting data into a front end application.
Depending on how you're accessing the data, you're better off either returning multiple rows per user as you have or retrieving the children (and emails, etc.) for each user as you need them. The key here is to only retrieve them if you need them. I believe that this is known as Lazy Loading in the Object Oriented world.
If you're doing this to fill a list box of some kind, and therefore you need them for each user, then you might consider setting some limit on the number of children that you'll retrieve based on how your list will appear and then use LEFT JOINs to get exactly that number for the rows that you retrieve, rather than doing the round trip to the server for every user.
In other words, it all depends. :)
It's a bit difficult getting my problem into a short form, so I apologise if the title doesn't make sense.
Anyway, here is the problem:
$query = '
SELECT issues.*, comments.author AS commentauthor, favorites.userid AS favorited FROM issues
LEFT JOIN comments ON comments.issue = issues.id AND comments.when_posted = issues.when_updated
LEFT JOIN favorites ON favorites.ticketid = issues.id AND favorites.userid = \'' . $_SESSION['uid'] . '\'
' . $whereclause . '
ORDER BY issues.when_updated ' . $order;
Don't mind the fact that it's PHP as I am not asking for PHP help.
The query retrieves a bunch of issues, and what I'm wishing to do is obtain the row count of favorites that have favorites.ticketid matching issues.id. My use of LEFT JOIN favorites is not to get what I've just mentioned, but instead to obtain whether the client has favourited the issue, thus the part favorites.userid AS favorited.
I have tried doing the following: (all at once, I'm putting this in bulleted form for readibility)
duplicating the existing LEFT JOIN favorites and removing the user id check from the duplicate
adding , COUNT(favorites.ticketid) AS favoritescount to the SELECT section
adding AS favorited to the original LEFT JOIN as well as changing favorites.userid to favorited.userid
With that attempt, my query ends up returning only one row.
SELECT issues.*,
comments.author AS commentauthor,
favorites.userid AS favorited,
(
SELECT COUNT(favorites.id)
FROM favorites
WHERE ticketid = issues.id
) AS numfavorites
FROM issues
LEFT JOIN comments
ON comments.issue = issues.id
AND comments.when_posted = issues.when_updated
LEFT JOIN favorites
ON favorites.ticketid = issues.id
AND favorites.userid = ?uid
That should work, I'm just using a subquery to get number of favourites