i have a table user
id firstname lastname
1 | Kenny | Kim
2 | Smith | Lt
3 | James | Wagh
4 | Wales | St
5 | Stephen | Mathew
teams table
tid manager teams
1 2 3
2 2 4
3 5 1
I want the result to be
Manager Team Mates
Smith Lt James Wagh, Wales St, Kenny Kim
I am not getting how to join. plz do suggest.
select concat(m.firstname, ' ', m.lastname) as Manager,
group_concat(concat(u.firstname, ' ', u.lastname) separator ', ') as `Team Mates`
from teams t
join user m on m.id = t.manager
join user u on u.id = t.teams
group by t.manager
Study Joins in Mysql, and try to solve your problem yourself. The Following link can help you a lot.
Joins in mysql
Related
My issue is the next one.
I have 3 tables: People, Cars and Driven:
People:
Id Name
1 | Tom
2 | James
3 | Charles
4 | Eric
5 | Thomas
6 | Robert
7 | Kim
8 | Ellias
Cars:
Id Name
1 | Ford
2 | Nissan
3 | Hyundai
Driven:
PID CID (People ID & Car ID)
1 | 1
2 | 1
5 | 1
5 | 2
6 | 1
6 | 2
7 | 1
7 | 2
7 | 3
8 | 1
I Want to retrieve pairs of people that driven the SAME SET OF CARS.
I mean: if Tom driven only Ford and James driven also ONLY Ford, i want to return this pair Tom/James as result. Also i want to include pairs of people that didn't driven any car (ie. Charles/Eric (0 cars driven both)).
The query result with the example above should return two columns per result, for example:
Name Name
Tom | James (Only Ford)
Tom | Ellias (Only Ford)
James | Ellias (Only Ford)
Charles | Eric (None BOTH)
Thomas | Robert (Ford and Nissan BOTH)
Also notice that Kim has driven Ford, Nissan and Hyundai. So Kim is not going to be pair with anybody. Tom James and Ellias all are driven Ford, so they are pair with themselves.
I'm tried with cartesian product and relational division, but I didn't find a solution. If someone can help me at least with a tip i will be really grateful. Thanks!
You can use the following query:
SELECT p.Id, p.Name,
COALESCE(GROUP_CONCAT(c.Name ORDER BY c.Name), 'None') AS cars_driven
FROM People AS p
LEFT JOIN Driven AS d ON p.Id = d.PID
LEFT JOIN Cars AS c ON c.Id = d.CID
GROUP BY p.Id, p.Name;
to get the list of cars driven per person.
Output:
Id Name cars_driven
-----------------------
1 Tom Ford
2 James Ford
3 Charles None
4 Eric None
5 Thomas Ford,Nissan
6 Robert Ford,Nissan
7 Kim Ford,Hyundai,Nissan
8 Ellias Ford
Using the above query twice as a derived table you can get the required result:
SELECT t1.Name, t2.Name, t1.cars_driven
FROM (
SELECT p.Id, p.Name,
COALESCE(GROUP_CONCAT(c.Name ORDER BY c.Name), 'None') AS cars_driven
FROM People AS p
LEFT JOIN Driven AS d ON p.Id = d.PID
LEFT JOIN Cars AS c ON c.Id = d.CID
GROUP BY p.Id, p.Name) AS t1
JOIN (
SELECT p.Id, p.Name,
COALESCE(GROUP_CONCAT(c.Name ORDER BY c.Name), 'None') AS cars_driven
FROM People AS p
LEFT JOIN Driven AS d ON p.Id = d.PID
LEFT JOIN Cars AS c ON c.Id = d.CID
GROUP BY p.Id, p.Name
) AS t2 ON t1.Id < t2.Id AND t1.cars_driven = t2.cars_driven;
Output:
Name Name cars_driven
----------------------------
Tom James Ford
Charles Eric None
Thomas Robert Ford,Nissan
Tom Ellias Ford
James Ellias Ford
Demo here
well, i have 100 student lists and every student has more than a hooby.
i have 2 tables,
table name = students
attribute = student_id,name,dob,address
and
table name = hobbies
attribute = hobby_id, student_id, hoby_name.
how do i get result like below.
.student_id | name | dob | address | hobby.
1 | Jordan | 12-12-1998 | 23 avenue |reading, dota2, football
2 | Bela | 13-01-1997 | 12 hills |swimming, badminton
3 | Jack | 01-02-1999 | 07 clinton|dota2
once i try to use subquery it says "subquery returns more than 1 row".
thank u guys.
select sa.student_id,name,dob,address, GROUP_CONCAT(hoby_name)
from students_attribute sa
left join hobbies_attribute ha
on sa.student_id = ha.student_id
group by sa.student_id
This will provide to required result.
I'm not a programmer and I read alot from this form about how to solve my question, but my search was no good
I have two tables
TABLE 1: members
id*| name | surname
-------------------
1 | Joe | Smith
2 | Mary | Sullivan
3 | Will | Stevenson
TABLE 2: messages
---------------------------------
id_message*| from | to | message
---------------------------------
1 | 2 | 1 | test
2 | 1 | 2 | re:test
3 | 3 | 1 | hi
*auto-increment fields
I wish to do a query where lists all the messages as shown below:
Mary Sullivan | Joe Smith | test
Joe Smith | Mary Sullivan | re:test
Will Stevenson | Joe Smith | hi
I'm really really really lost
Anyone can help? Thanks!
You need to join the members table 2 times with messages
select
concat(mem1.name,' ',mem1.surname) as `from_name`,
concat(mem2.name,' ',mem2.surname) as `to_name`,
m.message
from messages m
join members mem1 on mem1.id = m.`from`
join members mem2 on mem2.id = m.`to`
Try:
select from1.name+" "+from1.surname, to2.name+" "+to2.surname, message from table2
join table1 from1 on table2.`from` = table1.id
join table1 to2 on table2.`to` = table1.id
You need to write a Select with alias to refer members table 2 times:
SELECT CONCAT(M1.surname, ' ', M1.name) as FROM, CONCAT( M2.surname, ' ', M2.name) as TO FROM
members M1 INNER JOIN
messages M on M.from = M1.id
INNER JOIN messages M2 on M.to = M2.id
Database: MySQL
Table: Teams
TeamID INT
TeamName VARCHAR(20)
Table: People
PeopleID INT
FirstNameID INT
LastNameID INT
Table: TeamMembers
TeamID INT
PeopleID INT
Table Example: Teams
TeamID TeamName
1 Team Xstream
2 Team INsanity
Table Example: People
PeopleID FirstNameID LastNameID
1 1351 453
2 5463 763
3 976 8762
4 87 784
5 187 465
6 761 566
7 376 2134
Table Example: TeamMembers
TeamID PeopleID
1 1
1 3
1 7
2 2
2 4
2 5
2 6
Desired Output:
TeamName TeamMembers
Team Xstream John Smith/Jane Doe/Daniel Davis
Team INsanity Sally Sue/Tom Thomas/Jack Jones/Harry Henderson
There will not be a set number of TeamMembers per team, so it's not like I could have three subqueries because there will only be three team members. I've lightly looked online, but I always get the best and most thorough answers here. Any ideas or pointers, please let me know. I honestly have no idea where to begin here. Thanks.
I am guessing that the names of the people are actually stored somewhere considering you are just showing ID numbers as names. But you will want to use both CONCAT() and GROUP_CONCAT() for this result. The first step, will join all of the tables and use the CONCAT() function:
select t.teamname,
concat(p.FirstNameId, ' ', p.LastNameId) teamMembers
from teams t
left join teammembers m
on t.teamid = m.teamid
left join people p
on m.peopleid = p.peopleid;
See SQL Fiddle with Demo, which will produce the result:
| TEAMNAME | TEAMMEMBERS |
-------------------------------
| Team Xstream | 1351 453 |
| Team Xstream | 976 8762 |
| Team Xstream | 376 2134 |
| Team INsanity | 5463 763 |
| Team INsanity | 87 784 |
| Team INsanity | 187 465 |
| Team INsanity | 761 566 |
Once you have the data, then apply the GROUP_CONCAT() function and GROUP BY the teamname:
select t.teamname,
group_concat(concat(p.FirstNameId, ' ', p.LastNameId) SEPARATOR '/') teamMembers
from teams t
left join teammembers m
on t.teamid = m.teamid
left join people p
on m.peopleid = p.peopleid
group by t.teamname;
See SQL Fiddle with Demo
results:
| TEAMNAME | TEAMMEMBERS |
---------------------------------------------------
| Team INsanity | 761 566/87 784/187 465/5463 763 |
| Team Xstream | 976 8762/376 2134/1351 453 |
You didn't provide information on the table where names are actually stored, but what you are looking for is GROUP_CONCAT function. This is how you would use it to show the first and last name id's. I will leave it to you to join the name table and replace the name id fields in the query with the actual name fields.
SELECT t.TeamName, GROUP_CONCAT(CONCAT(p.FirstNameID, ' ', p.LastNameID))
FROM Teams as t
INNER JOIN TeamMembers as tm on t.TeamID = tm.TeamID
INNER JOIN People as p on tm.PeopleID = p.PeopleID
GROUP BY t.TeamName
By the way, normalizing the names out into their own table and using nameID seems like probably a good example of over normalization. Why not just have name values in People table? This will save you adding a fourth table to your query (and possibly fifth table if first and last names are in different tables).
The case:
I have 2 tables, 'contracts' and 'members' tied with contract.id = members.cid.
Each contract has one main member and several secondary members (usually the children and spouse of the main member). The main member's details (name, address etc) are stored in table contracts whereas, extra members details are kept in table members. (bad logic, i know but this was a mess to begin with and i need time to redesign the db and website)
The desired output:
When I run a batch print of all contracts (lets say, every Friday) I need to also print a copy of the contract for each member, too but with the member's details on the contract instead of the main member.
The question:
How does this translate into a mysql query? Ok, its a left join, but how do I say "print data from table members instead of contracts for the joined rows"?
Main fields that occur in the 2 tables are name + surname, those should be enough for a draft query example.
Example tables and data:
contracts
-------------------------
id | name | surname |
-------------------------
1 | Tom | Jones |
2 | Jamie | Oliver |
members
--------------------------------
id | cid | name | surname |
--------------------------------
1 | 1 | Jack | Jones |
2 | 1 | Anne | Jones |
3 | 2 | Cathy | Wilson |
So the results I want shoudld be:
cid | name | surname |
--------------------------
1 | Tom | Jones |
1 | Jack | Jones |
1 | Anne | Jones |
2 | Jamie | Oliver |
2 | Cathy | Wilson |
If i write
SELECT c.name as name, c.surname as surname, m.name as name, m.surname as surname
FROM contracts c
join members m on c.id = m.cid
I simply end up with
name and name_1, surname and surname_1 but I want ALL names to fall under name and likewise for all other matching columns.
Hope this works :::
select c1.id, c1.name, c1.surname
from contracts c1
union
(Select m.id, m.name, m.surname
from
members m left join contracts c on (c.id = m.cid))
This is what I finally did and it worked (field names are different but the syntax is what matters):
SELECT u.id, u.onoma_u, u.name_u,
coalesce(u.programa, aa.programa) as programa,
coalesce(u.barcode, aa.barcode) as barcode,
coalesce(u.plan, aa.plan) as plan,
coalesce(u.im_exp, aa.im_exp) as im_exp,
coalesce(u.symb, aa.symb) as symb
FROM (SELECT a1.id, a1.onoma_u, a1.name_u, a1.programa, a1.barcode, a1.plan, a1.im_exp, a1.symb
FROM aitisi a1
UNION
SELECT a2.id, m.name, m.surname, NULL, NULL, NULL, NULL, NULL
FROM members m
JOIN aitisi a2 ON a2.id = m.symbid) u
JOIN aitisi aa ON aa.id = u.id;
I used aliases and NULLS as dummy fields to fill in the blanks.