multi dimensional mysql select? - mysql

table: car_profile
carid, carname, caryear, cartype
table: user_profile
userid, username, useremail
table: user_car
id, carid, userid, status
FC: carid, userid
One carid can have multiple userid in table user_car (One car can be used by multiple persons)
Given a carid, I want to select caryear, cartype, [userid, username, useremail]. The one inside the bracket should be an array in itself, as multiple userid is possible. I am not sure if this is posisble?

SELECT can return a single value, a row of values, or a table of rows. There is no higher dimensional option. Below, the multiple userid, username, useremail will show up on separate rows.
SELECT cp.caryear, cp.cartype, up.userid, up.username, up.useremail
FROM car_profile as cp
LEFT JOIN user_car as uc
ON uc.carid = cp.carid
LEFT JOIN user_profile as up
ON uc.userid = up.userid
WHERE cp.carid = carid
ORDER BY cp.caryear, cp.cartype

Related

MySQL Multiple Joins 3 Table ( Bridge Table )

1st table Courses columns:
courseId|instructorId|desc|courseName|img|
2nd table StudentCourses columns:
studentCourseId|studentId
3rd table UserData columns:
userId|avatar|name
So knowing studentId,i am trying to get students owned courses with its instructors profiledata(in this case avatar and name)
result columns should be:
courseId,courseName,desc,img,instructorsname,instructorsavatar
This query gives me students courseInfo but instructor data is not included.
SELECT Courses.courseId,Courses.img,Courses.courseName,Courses.`desc` FROM Courses JOIN StudentCourses ON StudentCourses.studentCourseId = Courses.courseId WHERE StudentCourses.studentId ="igkw11tkwa06kpmoe9o6hyytrq0qaqjq"
assuming that instructorId match with userid
SELECT Courses.courseId
,Courses.img
,Courses.courseName
,Courses.`desc`
, u1.name
, u1.avatar
FROM Courses
INNER JOIN StudentCourses ON StudentCourses.studentCourseId = Courses.courseId
INNER JOIN UserData u1 ON u1.userId = Courses.instructorId
WHERE StudentCourses.studentId ="igkw11tkwa06kpmoe9o6hyytrq0qaqjq"

Mysql: count and group unique emails in associated tables by polymorphic join

The relevant part of my schema (Mysql 5.6.24) is thus:
table: training_event_invitees
registered (tinyint)
invitee_id (id)
invitee_type (varchar)
table: users
id (integer)
email (varchar)
school_id (integer)
table: contacts
id (integer)
email (varchar)
school_id (integer)
table: schools
id (integer)
email (varchar)
I want to try to do the following: get all the training_event_invitees that have registered set to 1, get the associated school, user and contact records, and then group them by school_id, and return the school id and the count of unique email addresses from that school.
training_event_invitees has a two-column foreign key, using invitee_id and invitee_type: invitee_type would be either "School", "User" or "Contact", and references the id field from the corresponding table.
So, algorithmically, it's something like
- get all the registered training_event_invitees
- get all of the associated user, contact and school records
- group these by users.school_id, contacts.school_id or schools.id
- count the number of distinct emails in each group
So, it should return an array like
[
[1234, 6],
[3407, 2]
]
where 1234 and 3407 are values of school_id and 6 and 2 are the count of distinct emails.
I can break this down into a few steps, but there must be a one-hit way to do it. Can anyone help?
One method is to combine the two tables using left join, and then doing the aggregation:
select coalesce(u.school_id, c.school_id) as school_id,
count(distinct coalesce(u.email, c.email)) as num_emails
from training_event_invitees tei left join
users u
on u.id = tei.invitee_id and tei.invitee_type = 'user' left join
contacts c
on c.id = tei.invitee_id and tei.invitee_type = 'contact'
where tei.registered = 1
group by coalesce(u.school_id, c.school_id);
EDIT:
To include the school, follow the same logic:
select coalesce(u.school_id, c.school_id, s.id) as school_id,
count(distinct coalesce(u.email, c.email, s.email)) as num_emails
from training_event_invitees tei left join
users u
on u.id = tei.invitee_id and tei.invitee_type = 'user' left join
contacts c
on c.id = tei.invitee_id and tei.invitee_type = 'contact' left join
schools s
on s.id = tei.invitee_id and tei.invitee_type = 'school'
where tei.registered = 1
group by coalesce(u.school_id, c.school_id, s.id);

Using Inner Join the same result is returned multiple times

I have used the following query:
OrderMaster table:
OrderId
UserId - F.K
Users table:
Id - P.K
Name
Email
Mobilenumber
Query:
SELECT
CAST(o.Id as varchar(50)) as ID,
CAST(o.Name as varchar(50)) as Name,
CAST(o.ContactNumber as varchar(50)) as Mobilenumber,
o.Email
FROM
ordermaster as t1
INNER JOIN
Users as o ON t1.UserId<> o.Id
The results look like this:
Id Name Mobilenumber Email
-------------------------------------------
1 xxxx 252548878 dfj#dkjf.com
2 yyyy 422557879 dsfsdf#kdjf.com
1 xxxx 252548878 dfj#dkjf.com
2 yyyy 422557879 dsfsdf#kdjf.com
Why is the same result returned multiple times? Please clarify me.
Since your join condition is <> and not =, each record in User join with all the records in Ordermaster that have a different id, giving the same result multiple times.
If you want to get all the Users which are not in Ordermaster then you can do:
SELECT ...
FROM Users u
WHERE NOT EXISTS (SELECT * FROM ordermaster WHERE u.Id = Id)

Mysql: count child tables for each user

I have four tables that contain some fields
1. user(id, name, email, password, .....)
2. policy1(id, userid, ....)
3. policy2(id, userid, ....)
4. policy3(id, userid, ....)
Now this is clear that user's primary key (id) is foreign key in other three tables.
I want to fetch total number of tables against each user that contain user id in that table.
for example:
user id 1 has entry in any two tables,
user id 2 has entry in three tables,
user id 3 has entry in any single table
Result should be like
id total
1 2
2 3
3 1
I tried using Left Join but that I could not get the expected result.
You can use a subquery to get the count for each user and then add the values together to get the final result. I am using a LEFT JOIN to return all user rows even if there is not a matching value in the other tables. If you only want the users with values in the other tables, then you can use an INNER JOIN:
select u.id,
Coalesce(CntP1, 0) + Coalesce(CntP2, 0) + Coalesce(CntP3, 0) TotalCount
from `user` u
left join
(
select count(*) CntP1, userid
from policy1
group by userid
) p1
on u.id = p1.userid
left join
(
select count(*) CntP2, userid
from policy2
group by userid
) p2
on u.id = p2.userid
left join
(
select count(*) CntP3, userid
from policy3
group by userid
) p3
on u.id = p3.userid

How to fetch a particular record from three tables

Table Name :: Feedback_master
Fields 1. feed_id 2. roll_id 3. batch_id 4. sem_id (semester ID) 5.f_id (faculty Id) 6. sub_id (subject Id) 7. remark. 8. b_id
Table Name :: subject_master
Fields
sub_id (subject Id)
sub_name (Subject Name0
f_id ( Faculty ID)
Table Name :: faculty_master
Fields
f_id (Faculty Id)
f_name (Faculty Name)
l_name (Faculty Name)
b_id
This are the three tables. Now I want to fetch the detail from this three table.
I want the output as
f_Name (faculty name), Sub_name (Subject Name ) , and remark (Remark ) when i give the (faculty id) f_id
could some one help me to over come this problem.
Using Objects
Select T1.f_name, T2.sub_name, T3.remark from faculty_master as T1,
subject_master as T2, Feedback_master as T3 where T1.f_id = 'your faculty_id'
and T1.f_id = T3.f_id and T2.sub_id = T3.sub_id
heu, MySQL I presume?
SELECT f_name, sub_name, remark
FROM faculty_master
LEFT JOIN subject_master USING(f_id)
LEFT JOIN Feedback_master USING(f_id)
WHERE f_id = the_id_you_want
select fm.f_name, sm.sub_name, remark from faculty_master fm left
join sub_master sm on fm.f_id=sm.f_id
left join feedback_master fbm on
sm.sub_id = fbm.sub_id
where fm.f_id= 123
You can build up the query in stages. The first thing is that you're after a list of feedback remarks, so start with this simple select query:
SELECT * FROM Feedback_master
That's listing all the feedback from all over, but you want to limit it to only feedback on a particular faculty, so let's add a Where clause:
SELECT * FROM Feedback_master
WHERE Feedback_master.f_id = #f_id
Now we've got the right list of records, but the list of fields is wrong. You want the faculty name and subject name, which aren't there in the Feedback_master table; the subject_master and faculty_master tables are linked and assuming that every remark has a subject ID and a faculty ID, we can use a simple inner join to link the tables:
SELECT * FROM Feedback_master
INNER JOIN subject_master ON Feedback_master.sub_id = subject_master.sub_id
INNER JOIN faculty_master ON Feedback_master.f_id = faculty_master.f_id
WHERE Feedback_master.f_id = #f_id
Now it's pulling out all the fields from all three table; this includes all the fields we need, so we can now simply name them in the Select clause:
SELECT
faculty_master.f_name, subject_master.sub_name, Feeback_master.remark
FROM Feedback_master
INNER JOIN subject_master ON Feedback_master.sub_id = subject_master.sub_id
INNER JOIN faculty_master ON Feedback_master.f_id = faculty_master.f_id
WHERE Feedback_master.f_id = #f_id