How do I write this SQL Query to join these three tables? - mysql

I have three tables that I need to query in order to get my results.
Table 1 has an AppId and a ProjectID
Table2 has an AppID and an AppName.
Table 3 has a ProjectID and a ProjectName
I want to get out of this a list, By AppName, the ProjectNames they are tied to.
So far, a basic query to get what I want works, but I only get the ID's. I need to somehow join these to get the names associated. I need to somehow join this to table2 with the project name information, and table 2 with the appname information.
Select * from Table1 ( this table has only ID's, not names)
Order by AppId

You can join the tables like this:
Select t2.AppName, t3.ProjectName
from table1 t1
inner join table2 t2 on t2.AppID = t1.AppID
inner join table3 t3 on t3.ProjectID = t1.ProjectID

Related

How to join where duplicates are needed?

I have a table structure like this
Table1
PersonID, referrer
Table2
Event_A_ID, PersonID, status
Table3
Event_B_ID, PersonID, status
I want to get the event status for everyone from table one with referrer=X by joining all of the event tables like Event_A...K and checking for PersonID. Since people can appear in multiple events we can have cases like this
PersonID=1001, EventA_ID, referrer=X, status
PersonID=1001, EventB_ID, referrer=X, status
PersonID=1001, EventK_ID, referrer=X, status
PersonID=1002, ...
PersonID=1003, ...
But I am not sure how to JOIN all of the event tables since the IDs can be duplicates (and are desired).
I tried to make a separate select and use the in syntax
...
WHERE 1=1
AND PersonID IN (SELECT PersonID from table1 where referrer=X)
But then I realized everything before will be an aggregate of events.
Should I start with the SELECT from Table1? Select the valid IDs first and then select from the events after? If so, how do I JOIN? I feel like ideal looks like union of all the event tables and then select
You can use union all for row wise implementation of data or you can also use inner joins between tables if there is not much table events. This will represent data in column format.
SELECT * FROM tb2 AS t2 INNER JOIN tb3 t3 ON t2.personId = t3.personId INNER JOIN tb1 t1 ON t1.personId = t2.personId WHERE t1.refer='refer1'
There can be many other approach too depending on the number of tables you want to join.
You should also consider to use a better relations among your db tables as your current scenario will lead you to have as many tables as many events you have. This will create slowness in retrieving the data for multiple events.
use union all and then apply join
select a.person_id,a.referrer,b.eventID,b.PersonID,b.status from Table1 a inner join
(
select Event_A_ID as eventID, PersonID, status from Table2
union all
select Event_B_ID as eventID, PersonID, status from Table3
)b on a.personid=b.personid
You can do something like this with left joins:
SELECT t1.PersonID, t1.referrer,
t2.Event_A_ID, t2.status as status_a,
t3.Event_B_ID, t3.status as status_b
.
.
.
FROM table1 t1
LEFT JOIN table2 t2 ON t2.PersonID = t1.PersonID
LEFT JOIN table3 t3 ON t3.PersonID = t1.PersonID
.
.
.
WHERE t1.referrer = 'x'

How to apply result of order by clause from subquery to main query

I have 2 tables which are linked by column named MID.
I want to fetch name from 1st table but the sequence is mentioned in 2nd table.
My query is as follows:
select name from table1 where MID in(select MID from table2 where CID="100" ORDER BY sequenceNum);
If i only run the query mentioned inside brackets then i get the data ordered by sequence.
But the above query is fetching the data from db as it is and not arranging it in sequence. What can be the problem?
I think this shoukld do the trick...
SELECT name FROM table1
INNER JOIN table2 ON Table2.MID = table1.MID AND CID="100"
ORDER BY
table2.sequenceNum
You want merge two tables and order results by merged table:
SELECT table1.name
FROM table1
LEFT JOIN table2 ON (table2.MID = table1.MID)
WHERE table2.CID = "100"
ORDER BY table2.sequenceNum;
or
SELECT table1.name
FROM table1
LEFT JOIN table2 ON (table2.MID = table1.MID AND table2.CID = "100")
ORDER BY table2.sequenceNum;
If you want get field from concrete table, use table prefix like table1.name

How do I select two tables in 1 query?

I want to select two tables in one query but it doesn't seem to work. I have tried nested select but I just got sql_error:
Subquery returns more than 1 row
Here is my query:
SELECT `client`.`id` as client_id,
(SELECT `org`.`name` FROM `org`) as organization
FROM `client`
What is the better way to query two tables?
Here is my expected result:
client_id = [1,2,3,4,5]
organization = [x,y,z]
This will work but it isn't what you want i think.
SELECT `client`.`id` as client_id, `org`.`name` as organization FROM `org`, `client`
This will give you a cross product of all rows in org with all rows in client. Maybe you want something like this:
SELECT `client`.`id` as client_id, `org`.`name` as organization FROM `org` JOIN `client` ON `client`.`memberOf` = `org`.`id`
The JOIN will connect rows of both tables where column memberOf in table client is equal to column id in table org
You should use join queries to join two or more tables. you can visit https://dev.mysql.com/doc/refman/5.0/en/join.html
Example of the join query:
SELECT t1.name, t2.salary
FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;

join with latest records only from another table:- Remove replication

I have 2 table lets say table1 and table2 table 1 is the master data and table 2 is child. I have to find out records from both table corresponding to the given table1.id. But there are more than 1 record in table2 corresponding to table1.id. So I would like to filter the latest record first from table2 corresponding to table1.id then apply the join on filtered data from table2 and table1.
table1= location_grids
table2= local_ads
Following what I am doing ?
SELECT * FROM `location_grids`as l
LEFT JOIN `local_ads` la ON `la`.`location_grid_id` = `l`.`id`
Inner join (SELECT id, location_grid_id, max(booked_till) as maxbooked
from local_ads group by location_grid_id ) as b
on la. location_grid_id = b.location_grid_id
and la.booked_till = b.maxbooked
WHERE `l`.`location_id` = '1'
AND `l`.`flag` = 1
The query is returning me unfiltered data. How can I filter then apply join on data. Any help will be appreciated.
table1:- location-grid
table2:- local_ad
Finally got the solution :- I was searching for following query.
SELECT location_grid_id, user_id, booked_from, booked_till, purpose, category, duration, price, MAX( created )
FROM local_ads
GROUP BY location_grid_id
Here I wanted to run join with latest record of the particular id as many record must be exist in local_ads

Ordering with two tables

I'm doing an order on all fields of a table (depending on the user's choice), however one column of that table contains a category (stored as an abbreviation) and those abbreviations are defined in a second table. How can I sort by category name? Example of the table structures below:
Table 1
title | amount | category_abbreviation
Table 2 -> for category
category_name | category_abbreviation
Just join the tables and order on a field from the second table.
SELECT * from table1
INNER JOIN table2
ON table1.category_abbreviation = table2.category_abbreviation
ORDER BY table2.category_name
Create a view and run your query from that:
SELECT
Table1.title, Table1.amount, Table1.category_abbreviation, Table2.category_name
FROM
Table1
INNER JOIN Table2 ON Table1.category_abbreviation = Table2.category_abbreviation
and use that as your datasource. Or just use the SQL as your datasource, however you are doing it.
You don't have to select the Table2.category_name though if you didn't want to
try
SELECT t1.*
FROM Table_1 T1
JOIN Table_2 T2
ON T1.category_abbreviation=T2.category_abbreviation
ORDER BY T2.category_name