I'm trying to join multiple table to get single row result for each id. This result will be send to angular UI
I have 3 tables user, friends and trip
A user can have multiple friends but one trip
I like to get all details corresponding to a user in one row, probably friends as field like an array?
This is how my table looks.
http://sqlfiddle.com/#!9/0879d/2
https://gist.github.com/tomalex0/9dee4fff85583732e7d0
group_concat should do the trick for you:
SELECT u.*, t.*, friendlist
FROM user u
LEFT JOIN trip t ON u.id = t.user_id
LEFT JOIN (SELECT user_id, GROUP_CONCAT(CONCAT (name, '- ', email)) friendlist
FROM friends
GROUP BY user_id) f ON f.user_id = u.id
Related
I have three tables:
table: users / columns: user_id, email, username
table: activities / columns: object, type, like_count
table: activities2 / columns: object, target_type, subject, type
The like_count in activities table has no problem and I count all of that with this query:
SELECT SUM(activities.like_count) AS likes, users.user_id, users.email, users.username
FROM activities
INNER JOIN users
ON activities.subject = users.user_id
GROUP BY user_id
But there is another count in activities table which is being inserted(NOT UPDATED) each time some other types of posts liked and I counted them by this query:
SELECT activities.subject, activities.object, COUNT(activities.type) AS likes
FROM activities
INNER JOIN activities2
ON activities.object = activities2.object AND activities2.target_type = 'parent'
WHERE activities2.type LIKE 'like_%'
GROUP BY activities2.subject
BUT the problem starts from here when I want to join them together! I want to count like_count + count of likes that inserted in that table(activities2) that contains string of 'like_' and beside this I want to join the subject(in activities table) which is related to user_id in the other table(users).
My last query is this:
SELECT users.user_id, users.email, users.username, activities.object, COUNT(activities.type)+SUM(activities.like_count) AS likes
FROM activities
INNER JOIN activities2
ON activities.object = activities2.object AND activities2.target_type = 'parent'
INNER JOIN users
ON activities.subject = users.user_id
WHERE activities2.type LIKE 'like_%'
GROUP BY users.user_id
The problem is when joining, it fetches the user information not for the one that I want.
In conclusion I want something like this:
user_id------email-----------------username----------object-------likes
2521---------a#b.com---------------abc---------------9578---------3
5484---------c#d.com---------------def---------------8547---------16
8431---------e#f.com---------------ghi---------------4584---------1
And offcourse the result is this but only likes are correct however columns of user_id, email, username that are in users table NOT!
I was wondering if you would help to fix it. I'm really tired of trying and facing to no result :(
users table data:
user_id-------------email---------------username
1-------------------a#b.com-------------abc
2-------------------c#d.com-------------def
3-------------------e#f.com-------------ghi
activities table data:
object----------type----------------like_count------subject
20--------------like_video----------0---------------1
20--------------like_photo----------0---------------2
33--------------like_music----------0---------------3
33--------------some_other_type-----5---------------6
33--------------some_other_type-----8---------------10
activities2 table data:
object------target_type-----subject-----type
20----------parent----------30----------like_video
21----------owner-----------40----------like_audio
22----------parent----------50----------something_not_start_with_like_
I want:
user_id------email-----------------username----------object-------likes
1------------a#b.com---------------abc---------------9578---------(sum of like_count + count of type which has like_ in first characters)
2------------c#d.com---------------def---------------8547---------(sum of like_count + count of type which has like_ in first characters)
3------------e#f.com---------------ghi---------------4584---------(sum of like_count + count of type which has like_ in first characters)
Consider joining the aggregate queries using derived tables and then run your addition calculation in the outer query. Also, below object column is removed from the second aggregate query's GROUP BY clause but still used in JOIN since you need summation at the subject level.
SELECT u.user_id, u.email, u.username, a.`object`, u.likes + a.likes as `total_likes`
FROM
(SELECT SUM(activities.like_count) AS likes, users.user_id, users.email, users.username
FROM activities
INNER JOIN users
ON activities.subject = users.user_id
GROUP BY user_id, users.email, users.username) u
INNER JOIN
(SELECT activities.subject, COUNT(activities.type) AS likes
FROM activities
INNER JOIN activities2
ON activities.object = activities2.object AND activities2.target_type = 'parent'
WHERE activities2.type LIKE 'like_%'
GROUP BY activities2.subject) As a
ON u.user_id = a.subject
I have the following statement:
SELECT
a.user_id AS user_id,
a.user_name AS user_name,
GROUP_CONCAT(b.course_id SEPARATOR ',') AS course_ids
FROM
user_courses b JOIN user a ON b.user_id_id = a.user_id
GROUP BY
a.user_id;
I want it to return all users and their courses, I also want it to return a row for users with no courses. However the result only contains users who have at least one course. How can I get ot to return all users even if they have no courses in the user_courses table?
You need a left join (and to put user first):
SELECT u.user_id, u.user_name,
GROUP_CONCAT(uc.course_id SEPARATOR ',') AS course_ids
FROM user u LEFT JOIN
user_courses uc
ON u.user_id_id = uc.user_id
GROUP BY u.user_id;
Notice that the query is easier to understand when you use abbreviations for table aliases instead of meaningless letters such as a and b.
I have two table user and follow. I want to write view such that it will fetch all details of particular user along with that two extra column as follower count and followee count alias.
eg. user id=11 then all details from user tables plus followcount 1 and followed count1
SELECT u.id,
u.userid,
u.name,
u.mobile,
(SELECT Count(*)
FROM follow f
WHERE f.followerid = u.userid) AS follower,
(SELECT Count(*)
FROM follow f
WHERE f.followeeid = u.userid) AS followee
FROM users u
You can achieve this is by using JOIN statements in your query:
example of how you can achieve your final result:
CREATE VIEW [Followers] AS
SELECT a.name, a.email, a.mobile, COUNT(SELECT COUNT(followerID) FROM follow WHERE followerID = a.userid), COUNT(SELECT COUNT(followeeID) FROM follow WHERE followeeID = a.userid) FROM users a INNER JOIN follow b ON b.followerID = a.userid
So, the two tables in question:
userinfo: id(PK), users_id(FK to users table), name, surname
doctorpatient: id(PK), doctor_id(FK to users table), patient_id(FK to users table)
The idea is each doctor is assigned a few patients via the doctorpatient table. What I want to do is return an array of arrays, where each of the inner arrays contains this:
users_id(doctor), name(doctor), surname(doctor), users_id(patient), name(patient), surname(patient)
Can this even be done using purely SQL? I tried this:
SELECT userinfo.users_id,
userinfo.name,
userinfo.surname,
u2.users_id,
u2.name,
u2.surname
FROM doctorpatient
RIGHT OUTER JOIN userinfo
ON doctorpatient.doctor_id = userinfo.users_id
LEFT OUTER JOIN userinfo AS u2
ON doctorpatient.patient_id = u2.users_id
but no matter what combination of joins I try, it never comes out right. I tried getting the data in three separate queries and then somehow get the result I need using PHP, but I got nowhere with that.
Edit: What I want is this:
array(
subarray1(patient_id1,
patient_name1,
patient_surname1,
doctor_id1,
doctor_name1,
doctor_surname1)
subarray2(patient_id2,
patient_name2,
patient_surname2,
doctor_id1,
doctor_name1,
doctor_surname1)
etc...
where one doctor can have multiple patients. What my query gets me looks something like this:
array(
subarray1(patient_id1,
patient_name1,
patient_surname1,
)
subarray2(patient_id2,
patient_name2,
patient_surname2,
)
etc...
But most of the data is null.
I think a simple JOIN may be sufficient. The OUTER JOINs appear to be causing the null values because it tries to treat the doctors as patients.
SELECT u1.users_id AS doctor_id,
u1.name AS doctor_name,
u1.surname AS doctor_surname,
u2.users_id AS patient_id,
u2.name AS patient_name,
u2.surname AS patient_surname
FROM doctorpatient AS d JOIN userinfo AS u1 ON d.doctor_id = u1.users_id
JOIN userinfo AS u2 ON d.patient_id = u2.users_id
Try this:
SELECT
u.id as user_id,
u.name as user_name
u.surname as user_usrname
d.id as doc_id,
d.name as doc_name,
d.surname as doc_surname
FROM doctorpatient as dp
LEFT JOIN userinfo as u ON (dp.pacient_id = u.id)
LEFT JOIN userinfo as d ON (dp.doctor_id = d.id)
I am trying to figure out a complex (at least for me!) mysql query and hoped someone here might have some clue's for me..
I have 2 tables "Users" and "files".
Users:
id, name, address, etc..
Files:
id, user_id, file_name, etc..
I want to select all rows from Users and in the result create a last column that has a count of all file with where User.id = Files.user_id.
I tried SELECT * from Users UNION(SELECT COUNT Files.user_id WHERE Users.id = Files.user_id) but doesn't work of course..
select u.*, count(f.id)
from users u
left join files f on u.id = f.user_id
group by u.id