How to get the ID if condition is met in multiple table - mysql

i've tables like
personal info:
uid name location relation
12 ario 32 1
13 arvin 32 4
Professional Info
uid inst pos
12 SER admin
13 Wipro data analyzer
now, how to get the uid who works in Wipro, with relation of 4 (which is 13)

select * from `personal info` p inner join `Professional Info` pi
on p.uid = pi.uid
where p.relation = 4

Related

mySQL - Multiple rows into one row

I need data from multiple tables using joins. Below are the tables.
table 1 : list_vehicles
pk_vehicle_id vehicle_reg_no vehicle_type
1 REG1 Bus
2 1 Bus
7 1 Bus
table 2 : list_vehicles
pk_route_id route_code route_name route_description
26 CODE1 Route1 First Route
27 CODE2 Route2 Second Route
28 CODE3 Route3 Third Route
table 3 : tbl_route_vehicle_mgmt
pk_route_veh_id fk_route_id fk_vehicle_id
4 22 2
5 23 1
6 27 1
table 4: tbl_staff_allocation
pk_id fk_route_id fk_staff_id staff_type
1 27 13 Attendant
2 27 14 Driver
3 27 15 Conductor
I need the following data from the above tables, say for pk_route_id =27
Route_Name Vehicle_Number Vehicle_Type Driver_Id Attendant_Id Conductor
Route 2 REG1 Bus 13 14 15
I tried to write a part of the SQL as follows. I am stuck and not sure how to enhance it to get the required results.
SELECT a.route_code,a.route_name,a.route_description, tbl_b.fk_vehicle_id,tbl_c.fk_staff_id,tbl_c.staff_type, tbl_c.fk_route_id
FROM `list_routes` AS a
INNER JOIN tbl_route_vehicle_mgmt AS tbl_b
ON a.pk_route_id = tbl_b.fk_route_id
INNER JOIN tbl_staff_allocation AS tbl_c
ON a.pk_route_id = tbl_c.fk_route_id
where a.pk_route_id =27 AND (tbl_c.staff_type ="Driver" OR tbl_c.staff_type ="Conductot" OR tbl_c.staff_type ="Attendant" )
Can anyone please help me with the SQL to get the required data.
You should use several self join on tbl_staff_allocation
SELECT
a.route_code
,a.route_name
,a.route_description
,tbl_b.fk_vehicle_id
,tbl_c1.fk_route_id
,tbl_c1.fk_staff_id as Attendant_id
,tbl_c2.fk_staff_id as Driver_id
,tbl_c3.fk_staff_id as Conductor_id
FROM `list_routes` AS a
INNER JOIN tbl_route_vehicle_mgmt AS tbl_b ON a.pk_route_id = tbl_b.fk_route_id
INNER JOIN tbl_staff_allocation AS tbl_c1 ON a.pk_route_id = tbl_c1.fk_route_id and tbl_c1.staff_type ='Attendant'
INNER JOIN tbl_staff_allocation AS tbl_c2 ON a.pk_route_id = tbl_c2.fk_route_id and tbl_c.staff_type ='Driver'
INNER JOIN tbl_staff_allocation AS tbl_c3 ON a.pk_route_id = tbl_c3.fk_route_id and tbl_c.staff_type ='Conductor'
INNER JOIN list_vehicles AS d on d.pk_vehicle_id = tbl_b.fk_vehicle_id

Get data from two tables using by data from another table in SQL Database

I have got 3 tables on my database. Lets assume they are named as ACC, POS, CON.
At ACC table i have got ID and NAME columns. At POS table, there is LAT and LONG columns and at CON table, AccID and FrID columns.
to give an example.
ACC / POS / CON
ID NAME / ID LAT LONG / AccID FrID
1 Mike 1 10 15 1 2
2 Bob 2 20 25 1 4
3 Jack 3 18 21 2 3
4 Rocky 4 37 45 2 1
This is the data from my 3 tables. Now i want to select all LAT and LONG values from POS and NAME values from NAME where AccID in FrID.
To be more specific, i want to check for Mike's friend's Lat Long. At Con table there is two friends of Mike, ID=2 and ID=4 so i want to get a table like.
ACC_NAME LAT LONG
Bob 20 25
Rocky 37 45
can you give me a query example for this problem. Thank you.
You can try by using INNER JOIN. Here is full Query:-
SELECT ACC.Name, POS.LAT, POS.LONG FROM CON
INNER JOIN ACC ON CON.FrID = ACC.ID
INNER JOIN POS ON POS.ID = ACC.ID
WHERE CON.AccID = 1
Explanation Here
Get All friends of Mike By
SELECT * FROM CON
WHERE CON.AccID = 1 -- Mike Account Id
Get Friends Name by Join with Acc Table
SELECT ACC.Name FROM CON
INNER JOIN ACC ON CON.FrID = ACC.ID
WHERE CON.AccID = 1
Get Lat ang Long by Join with POS Table
SELECT ACC.Name, POS.LAT, POS.LONG FROM CON
INNER JOIN ACC ON CON.FrID = ACC.ID
INNER JOIN POS ON POS.ID = ACC.ID
WHERE CON.AccID = 1

MySQL: multiple counts with join

Here are my two tables:
[items]
- id - model - location_id -
1 mA 23
2 mA 23
3 mA 23
4 mB 24
5 mB 24
6 mC 25
7 mC 26
[locations]
- id - name -
23 aisle-3
24 aisle-4
25 aisle-5
26 aisle-6
I am trying to query the locations table for the location names and also bring back a count of the items at that location. Here is something I tried to no avail:
SELECT name, COUNT(item.id)
FROM locations
INNER JOIN items AS item ON (item.location_id = locations.id)
Can anyone help me with this?
You forgot to GROUP BY:
SELECT l.*, COUNT(item.id)
FROM locations l
INNER JOIN items AS i
ON i.location_id = l.id
GROUP BY l.id
And if you want to get COUNT() even when there is no item assigned to that location you should LEFT JOIN instead of INNER JOIN.

MySQL Left Join depending on Two Fields

Let's assume we have 2 tables:
First table: "members"
id name
===========
10 Rooney
20 George
30 Hoytt
40 Percy
Second table: "interactions"
id iType mem1 mem2
===========================
5501 PRIVMSG 10 30
5502 NOTICE 20 40
And the result should be:
id iType mem1 mem2 mem1name mem2name
==========================================
5501 PRIVMSG 10 30 Rooney Hoyyt
5502 NOTICE 20 40 George Percy
How can we achive this output table using a single MySQL query?
Thanks by now.
Just use two joins and give them differen aliasses like this:
SELECT interactions.*,m1.name as 'mem1name',m2.name as 'mem2name'
FROM
interactions
LEFT JOIN members m1 ON (interactions.mem1 = m1.id)
LEFT JOIN members m2 ON (interactions.mem2 = m2.id)

Multiple Counts from many INNER JOIN tables with Conditions

I'm having a lot of trouble figuring out how to write this query. Here's an exmaple of the data set and what I need to query:
**System Table**
SystemID Active
1 T
2 T
3 T
4 F
5 F
6 F
**BlogPost Table**
BlogPostID SystemID Create_Month
100 2 Jan
101 2 Jan
102 2 Feb
103 3 Feb
104 3 Mar
105 6 Mar
106 6 Mar
**Comment Table**
Comment ID BlogPostID Liked
201 100 T
202 100 T
203 100 T
204 102 T
205 102 T
206 102 T
207 103 F
So, In words, I'm trying to get: By month, show me all the active systems who created a post during that month, the number of posts they made in aggregate, and the count of the subset of those posts who had a comment that was like.
The end result would be like:
Column 1 - Month
Column 2 - Count of Active Systems where a Post Created in Month
Column 3 - Count of Posts Applicable to those systems
Column 4 - Count of Applicable Posts that had comments that were liked
I don't even know where to start really. My terrible "this is obviously wrong" attempt is below. Any help is much appreciated, thanks!
SELECT
Month,
COUNT(DISTINCT system.systemid),
COUNT(blogpost.BlogPostID)
COUNT(comments.commentiD)
FROM
system INNER JOIN
blogpost ON system.systemid = blogpost.systemid INNER JOIN
comments ON blogpost.BlogPostID = comment.BlogPostID
WHERE
system.active = T
AND comments.like = T
GROUP BY month
A complicated one !
SELECT
b.Create_Month,
COUNT(DISTINCT s.SystemID) as SystemCount,
COUNT(DISTINCT b.BlogPostID) as PostsCount,
COUNT(DISTINCT t.BlogPostID) as PostsWithLike
FROM System s
JOIN BlogPost b
ON s.systemID = b.systemID
AND s.Active = 'T'
LEFT JOIN Comment c
ON b.BlogPostID = c.BlogPostID
LEFT JOIN
(
SELECT DISTINCT c.BlogPostID as BlogPostID
FROM
Comment c
GROUP BY c.BlogPostID
HAVING SUM(if(c.Liked='T',1,0))>0
) as t
ON b.BlogPostID = t.BlogPostID
GROUP BY b.Create_Month
This is probably what you want :
SELECT s.systemid, active, bp.create_month, bp.systemid, COUNT(bp.blogpostid), COUNT(c.liked)
FROM system AS s
LEFT OUTER JOIN Blogpost AS bp ON s.systemid = bp.systemid
LEFT OUTER JOIN Comment AS c ON bp.blogpostid = c.blogpostid
WHERE active = 'T' AND c.Liked = 'T' GROUP BY s.systemid,bp.create_month