On my database when i do this query:
SELECT *
FROM users u
LEFT JOIN usersToStrategy uts on uts.userID = u.userID
LEFT JOIN strategy s on uts.stratID = s.stratID
I get the following results:
userID username fName utsID userID stratID stratID stratName
1 nlubin Neal 66 1 4 4 s3
1 nlubin Neal 65 1 3 3 s5
1 nlubin Neal 64 1 2 2 s2
1 nlubin Neal 63 1 1 1 s1
How do I structure my query that instead of getting those four rows (for only one user) I get one row for the user with all of the user's strats in one cell in the row like this:
userID username fName stratNames
1 nlubin Neal s3, s5, s2, s1
I can give you as much information that I can within reason to help with the question.
I think you are looking for GROUP_CONCAT
SELECT userID, username, fName, GROUP_CONCAT(stratNames SEPARATOR ', ') AS strats
FROM users u
LEFT JOIN usersToStrategy uts on uts.userID = u.userID
LEFT JOIN strategy s on uts.stratID = s.stratID
GROUP BY userID, username, fName
You use GROUP_CONCAT.
Use GROUP_CONCAT like this
SELECT userID,
username,
fName,
GROUP_CONCAT(stratNames)
FROM users u
LEFT JOIN usersToStrategy uts on uts.userID = u.userID
LEFT JOIN strategy s on uts.stratID = s.stratID
GROUP BY username;
Related
The database tables I am working with are as the following:
Type_Telephone
ID_Type_Telephone (PK)
Description
0
LandLine
1
Cellular
2
Telecopier
Telephone
ID_Telephone (PK)
ID_Client (FK)
ID_Type_Telephone (FK)
Numero
100
201
0
514-555-0165
101
201
1
514-555-0155
102
202
1
514-555-0176
103
200
0
514-555-0164
104
200
1
514-555-0119
Client
ID_Client (PK)
Nom
Prenom
200
Bertrand
Antoine
201
Legault
Claude
202
Leonard
Sylvie
I would like to write a SQL query that lists all customers' Cellular and Landline telephones. I would like to have only one row of results per customer such as:
Nom
Prenom
Landline
Cellular
Bertrand
Antoine
514-555-0164
514-555-0119
Legault
Claude
514-555-0165
514-555-0155
Leonard
Sylvie
514-555-0176
Any advice how to generate this ? Thank you !
This requires a pivot query, something like this:
SELECT
c.ID_Client,
c.Nom,
c.Prenom,
MAX(CASE WHEN tt.Description = 'LandLine' THEN t.Numero END) AS Landline,
MAX(CASE WHEN tt.Description = 'Cellular' THEN t.Numero END) AS Cellular
FROM Client c
LEFT JOIN Telephone t
ON t.ID_Client = c.ID_Client
LEFT JOIN Type_Telephone tt
ON tt.ID_Type_Telephone = t.ID_Type_Telephone
GROUP BY
c.ID_Client,
c.Nom,
c.Prenom;
If you also want to include possible telecopier numbers, then add another max of CASE expression to the above query.
SELECT c.nom,
c.prenom,
t0.numero landline,
t1.numero cellular
FROM Client c
LEFT JOIN Telephone t0 ON c.id = t0.ID_Client AND t0.ID_Type_Telephone = 0
LEFT JOIN Telephone t1 ON c.id = t1.ID_Client AND t1.ID_Type_Telephone = 1
If you need one more column for Telecopier then add one more table copy.
The query assumes that Telephone (ID_Client, ID_Type_Telephone) is defined as UNIQUE. If not then
SELECT c.nom,
c.prenom,
GROUP_CONCAT(t0.numero) landline,
GROUP_CONCAT(t1.numero) cellular
FROM Client c
LEFT JOIN Telephone t0 ON c.id = t0.ID_Client AND t0.ID_Type_Telephone = 0
LEFT JOIN Telephone t1 ON c.id = t1.ID_Client AND t1.ID_Type_Telephone = 1
GROUP BY 1, 2
T1 Customers
IDZ NAME MEGAID
123 TOM 32132
124 JEK 32323
125 MAX 32342
126 JIZ 32134
T2 Info:
ID CID GUNS STATUS
1 123 3 1
2 124 4 2
3 126 NULL 1
T3 Status:
ID TYPE
1 Active
2 Inactive
IDZ = CID
I need to return NAME, MEGAID and STATUS (Active/Inactive) for everyone who have NULL on GUNS column from INFO table.
I tried this:
SELECT Customers.Name, CustomersMEGAID, Status.TYPE
FROM Customers
LEFT JOIN Customers ON Info.CID=Custoners.IDZ
WHERE Info.Guns= IS NULL;
But thats doesnt work(
Big thanks if someone can help with this
your question is full with errors but here is a query for you:
SELECT Customers.Name, Customers.MEGAID, Status.TYPE
FROM Customers
LEFT JOIN Info ON Customers.IDZ = Info.CID
INNER JOIN Status ON Info.STATUS = Status.ID
WHERE Info.Guns IS NULL;
You can join all three tables, and then search for nulls in the column.
For example:
select
c.name,
c.megaid,
s.type
from customers c
join info i on i.cid = c.idz
join status s on s.id = i.status
where i.guns is null
hi guys i have a data from sql like
UserID UserInfo ImageName Point
159 Ozan Işık 1575972330250.jpeg 32
558 Mert Malaveş 1578248740672.jpeg 36
68 Egemen Yılmaz 1573696212462.jpeg 82
558 Mert Malaveş 1578248740678.jpeg 36
Here is my sql
SELECT DISTINCT Users.UserID,Users.UserInfo,BestPracticeParticipant.ImageName ,BestPracticeParticipant.Point
FROM BestPracticeParticipant
JOIN Users on Users.UserID=BestPracticeParticipant.UserID
WHERE BestPracticeParticipant.BestPracticeID=1
But i need to data like this
UserID UserInfo ImageName Point
159 Ozan Işık 1575972330250.jpeg 32
558 Mert Malaveş 1578248740672.jpeg 36
68 Egemen Yılmaz 1573696212462.jpeg 82
i need same UserID rows doesnt duplicate but i cant group by because of the other veriables from my sql.
Thank you.
If you want a random matching row, you can use row_number():
SELECT u.*
FROM (SELECT u.UserID, u.UserInfo, bpp.ImageName, bpp.Point,
ROW_NUMBER() OVER (PARTITION BY u.UserID ORDER BY rand()) as seqnum
FROM BestPracticeParticipant bpp JOIN
Users u
ON u.UserID = bpp.UserID
WHERE bpp.BestPracticeID = 1
) u
WHERE seqnum = 1;
Note: You can control which row you get by modifying the ROW_NUMBER().
EDIT: The above works on MySQL 8+. If you have an older version, then a unique id in the result set can be used to do what you want.
For instance, if ImageName is unique (for a given user):
SELECT u.UserID, u.UserInfo, bpp.ImageName, bpp.Point
FROM useres u JOIN
BestPracticeParticipant bpp
ON u.UserID = bpp.UserID
WHERE bpp.BestPracticeID = 1 AND
bpp.ImageName = (SELECT MAX(bpp2.ImageName)
FROM BestPracticeParticipant bpp2
WHERE bpp2.UserID = bpp.UserID AND
bpp2.BestPracticeID = 1
);
It seems your MySQL version doesn't support window functions. So from your sample data, You may try below query -
SELECT DISTINCT U.UserID,U.UserInfo,BPP.ImageName,BPP.Point
FROM (SELECT UserID, MIN(ImageName), Point
FROM BestPracticeParticipant
GROUP BY UserID, Point) BPP
JOIN Users U on U.UserID=BPP.UserID
WHERE BPP.BestPracticeID=1
I have two tables. The first one is customer information and the second is phone numbers.
So in the first table I would have:
ID Name
1 John
2 jill
In the second table I would have:
ID phone ext notes customerID
1 687-5309 20 Primary 1
2 687-5310 55 John's cell phone 1
3 687-5311 18 Note! Emergency Only! 1
4 235-1189 2
5 235-2324 24 title:owner 2
When I query it I would like it give me a multidimensional result from the right table. So the result would be:
[ID]=>1
[Name]=>John
[phoneList]=>[
[
[ID]=>1 , [phone]=>687-5309 , [ext]=>20 , [notes]=>Primary ],
[ID]=>2 , [phone]=>687-5310] , [ext]=>55 , [notes]=>John's cell phone ],
[ID]=>3 , [phone]=>687-5311] , [ext]=>18 , [notes]=>Note! Emergency Only! ],
]
]
So far this is as far as I can get:
SELECT *
FROM customer_info
LEFT JOIN (
SELECT *
FROM phone_numbers
) WHERE ID=1
I'm not even sure if this is possible. But it feels like it should be.
Without grouping on the customer_info it would just be a simple LEFT JOIN.
SELECT cust.*, phone.ID AS phone_id, phone.phone, phone.ext, phone.notes
FROM customer_info AS cust
LEFT JOIN phone_numbers AS phone ON phone.customerID = cust.ID
WHERE cust.ID = 1;
But if you want one record per customer_info ID?
Then you could also GROUP BY the customer_info and then use GROUP_CONCAT to get 1 string with the phone id's and the phone numbers.
SELECT cust.ID, cust.Name,
group_concat(concat(phone.ID,':',concat_ws(',', phone.phone, ifnull(phone.ext,''), phone.notes)) separator ';') AS phoneList
FROM customer_info AS cust
LEFT JOIN phone_numbers AS phone ON phone.customerID = cust.ID
WHERE cust.ID = 1
GROUP BY cust.ID, cust.Name;
And if the JSON_OBJECT (MariaDB, MySql) function is available in your version, then one could use that.
SELECT cust.ID, cust.Name,
group_concat(JSON_OBJECT('id', phone.ID, 'phone', phone.phone, 'ext', phone.ext, 'notes', phone.notes)) AS phoneList
FROM customer_info AS cust
LEFT JOIN phone_numbers AS phone ON phone.customerID = cust.ID
WHERE cust.ID = 1
GROUP BY cust.ID, cust.Name;
Test on db<>fiddle here
(The example that follows is hypothetical, but illustrates the concept).
Using MySQL, say I have 2 tables:
userFromID userToId moreInfo
1 2 cat
1 3 dog
4 1 bear
3 4 fish
And...
userId someInfo addlInfo
1 m 32
2 f 33
3 m 25
4 f 28
And I want to query for a user id, and get back joined info from both tables for all users that share a relationship with user1.
assume that the first table has something like alter table thatFirstTable add unique index(userFromId, userToId) so there won't be any duplicates - each relationship between the two ids will be unique.
it doesn't matter who's the "from" or "to"
so the desired result would be something like this, if queried for relationships with user id: 1
userId moreInfo someInfo addlInfo
2 cat f 33
3 dog m 25
4 bear f 28
Thanks.
/EDIT this "works" but I suspect there's a better way?
SELECT * FROM users JOIN friends ON friends.userFrom = users.id OR friends.userTo = users.id WHERE users.id != 1 AND friends.userFrom = 1 OR friends.userTo = 1
/EDIT2 - I updated the sample output to better reflect the goal
try this query::
select tbl2.userid,tbl1.moreinfo,
tbl2.someinfo,tbl2.addinfo
from tbl1 join tbl2
on (tbl1.usertoid = tbl2.userid and tbl1.userfromid = 1)
You should just join the tables with the query below.
select u.userId, f.moreInfo, u.someInfo, u.addlInfo
from users AS u INNER JOIN friends AS f ON u.userId = f.UserToId
where f.userFrom = 1
Try this. Tested and 100% working
select a.userToID, a.moreInfo, b.someInfo, b.addInfo from tbl1 a
left outer join
tbl2 b on a.userToID = b.userId
where a.userFromID = 1;