mySQL multiple inner joins - mysql

I have two tables I'm pulling information from.
Lets say table1 has following columns
(id, title, category, sub_category, sub_sub_category)
Lets say table2 has following columns
(category_id, category_name)
I have a select statement that so far looks as follows:
SELECT
table1.id,
table1.title,
table2.category_name as Cat1,
table2.category_name as Cat2,
table2.category_name as Cat3
FROM
table1,
table2
INNER JOIN table2 as c1 ON c1.category_id = table1.category
INNER JOIN table2 as c2 ON c2.category_id = table1.sub_category
INNER JOIN table2 as c3 ON c3.category_id = table1.sub_sub_category
WHERE
table1.id = ?
This gives me an error about table1.category being an unknown column
I have also tried
SELECT
table1.id,
table1.title,
table2.category_name as Cat1,
table2.category_name as Cat2,
table2.category_name as Cat3
FROM
table1,
table2
WHERE table1.id = ?
AND table1.category = table2.category_id
AND table1.sub_category = table2.category_id
AND table1.sub_sub_category = table2.category_id
The last example at least gives me column output I'm looking for which would be
(table1.id, table1.title, table1.category name, table1.sub_category name...)
So showing the category name from table 2 instead of the ID's. I am an amateur coder and haven't had to use inner joins before but maybe that is what I need to do. I just can't figure out how to get it to output the data I need.
Thank you in advance for your time and consideration.

Your problem is that you have a comma in the from clause. Simple rule: never use commas in the from clause. Always use explicit join syntax.
Then, you also have table2 mentioned an extra time, and your select is pulling columns from the wrong instance of table2.
The fixed up query looks like:
SELECT t1.id, t1.title,
c1.category_name as Cat1, c2.category_name as Cat2,
c3.category_name as Cat3
FROM table1 t1 INNER JOIN
table2 c1
ON c1.category_id = t1.category INNER JOIN
table2 c2
ON c2.category_id = t1.sub_category INNER JOIN
table2 c3
ON c3.category_id = t1.sub_sub_category
WHERE t1.id = ?;

Ups, you have a couple of problems here.
First table2 can have only one column called category_name so there is no reason to select it three times like you do, result of that would be that you have three absolutely same column with different name.
Second the syntax is completely wrong. Syntax for INNER JOIN is
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
also you need to have column in both table usually foreign key and that column will be use to match data. I guess in your case it should be category_id from second table and in first table that's column you named category (it, should be renamed at category_id it's more convenient)...
So if understand right you want to select id, title from first table and category_name from second you could do that like this:
SELECT table1.id, table1.title, table2.category_name
FROM table1
INNER JOIN table2
ON table1.category_id = table2.category_id;
And friendly advice to you is to find some online sources and learn a little bit more about this before you continue with what ever you do...

Related

SQL Inner Joins Giving Incorrect Count Values in MS ACCESS

I am trying to use join to connect multiple tables in MS Access to get count values. But I don;t know it gives wrong count values. If I try to join them individually, then it gives me correct count values.
I have 3 Tables. Table 2 and Table 3 are independent and are connected to Table 1. Test 2 and test 3 are basically text values and I want to count the rows .
Table1(ID1(Primary Key),Name)
Table2(ID2(Primary Key), ID1(Foreign Key), Test2)
Table3 (ID3(Orimary Key), ID1(Foreign Key), Test3)
The Query that I get from MS Access is given below:
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2, Count(Table3.Test3) AS CountOfTest3
FROM (Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID2)
INNER JOIN Table3 ON Table1. ID1 = Table3.ID3
GROUP BY Table1.ID1;
But this gives me wrong Count Values.
Can someone please help me.
Thanks.
When I use it individually, it gives me correct count value:
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2
FROM Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID1
GROUP BY Table1.ID1;
SELECT Table1. ID1, Count(Table3.Test3) AS CountOfTest3
FROM Table1 INNER JOIN Table3 ON Table1.ID1 = Table3.ID1
GROUP BY Table1.ID1;
But when I try to join Table1, Table2 and Table 3 in MS Acces, it gives me incorrect count values.
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2, Count(Table3.Test3) AS CountOfTest3 FROM (Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID1) INNER JOIN Table3 ON Table1. ID1 = Table3.ID1 GROUP BY Table1.ID1
As per my understanding it is taking the count value of 1st query in the parenthesis and multiplying it with the count values of the other inner join.
I have tried many things but don't know what to do. Access adds parenthesis for some reason.
If I can assume test2 and test 3 are unique to each record (perhaps it would be better to count the PK?)
SELECT Table1.ID1
, Count(distinct Table2.Test2) AS CountOfTest2
, Count(distinct Table3.Test3) AS CountOfTest3
FROM Table1
INNER JOIN Table2
ON Table1.ID1 = Table2.ID2
INNER JOIN Table3
ON Table1.ID1 = Table3.ID3
GROUP BY Table1.ID1;
Or you may have to get the counts before the joins though the use of inline views. You could use window functions if MSSQL SERVER but Access needs the inline views.
SELECT A.ID1
, B.CountOfTest2
, C.CountOfTest3
FROM Table1 A
INNER JOIN (SELECT Table2.ID2, count(table2.test) as CountOfTest2
FROM Table2
GROUP BY Table2.id) B
ON Table1.ID1 = B.ID2
INNER JOIN (SELECT Table3.id, count(table3.test3) as CountOfTest3
FROM Table3
GROUP BY Table3.id) C
ON B.ID1 = C.ID3
GROUP BY A.ID1;
Yup i had the same problem when i was trying to do this. just use the double sql function to counteract the html and you should be good. once the query has been doubled it will react like a C++ quota statement. If this fails you can always just quantify the source fields to adhear to the table restrictions. its actually a piece of cake i hope this helped.

Mysql need to get one column two times from table1 and sort them by two different columns of table2

So here is the thing, I have two tables:
table1 has columns intUsersID, varUsersName
table2 has columns intCouriers, intResponsible
intCouriers (have some numbers of intUsersID that are Couriers), and intResponsible (have some numbers of intUsersID that are Responsible)
In my query I must see User Names of Couriers and of the Responsible persons
something like that:
SELECT
table1.varUsersName 'Couriers',
table1.varUsersName 'Responsible'
FROM
table1
LEFT JOIN
table2 ON table2.intCouriers = table1.intUsersID
And then I need some how to subquery or join this "table1.varUsersName 'Responsible'", to get also 'Reponsible' persons. Please help me.
Should be this
SELECT table1.varUsersName 'Couriers', table2.varUsersName 'Responsible'
FROM table1
INNER JOIN table3 on table1.intUsersID = table3.intCouriers
INNER JOIN table1 as Table2 on table2.intUsersID = table3. intResponsible
SELECT Couriers.varUsersName as "Couriers",
Responsible.varUsersName as "Responsible"
FROM `table2` t2
LEFT JOIN table1 Couriers on Couriers.intUsersID = t2.intCouriers
LEFT JOIN table1 Responsible on Responsible.intUsersID = t2.intResponsible

How to combine 2 columns from 2 tables and subtract duplicates

I have 2 tables t1 and t2. Each have a customer ID column. What I am looking for is to join the 2 columns and SUBTRACT the duplicates.
My EG:
Table1 and Table2 with the IDs for each
I have tried a union query. The result I am left with is ID = 1,2,3,4,5,6,7,8,9,10. Where, what I'm after is subtracting 1-5 from Table2 and the result = 6,7,8,9,10.
I hope that makes sense and that someone is able to help. Sorry if this is a bit too simple compared to what you're all used to.
In SQL Server you can use the EXCEPT operator:
select ID
from Table2
except
select ID
from Table1
Mysql does not support it though. Using a an in clause or a left join would work in both servers:
--Using In clause
SELECT ID
FROM Table2
WHERE ID NOT IN
(
SELECT ID
FROM Table1
);
--Using join
SELECT Table2.ID
FROM Table2
left join Table1
on Table2.ID = Table1.ID
where Table1.ID is null
Use left outer join
select * from t1 left outer join t2 on t1.customerid = t2.customerid

SQL Query select from many tables

I have 5 Tables like this:
TABLE 1: PRIMARY_KEY,NAME,FK_TABLE2
TABLE 2: PRIMARY_KEY,FK_TABLE3
TABLE 3: PRIMARY_KEY,FK_TABLE4
TABLE 4: PRIMARY_KEY,FK_TABLE5
TABLE 5: PRIMARY_KEY,DIAGRAM_NAME
And what I want is when I search for a name in a search bar, it returns Name from Table1, and also DIAGRAM_NAME from table 5.
The first part is easy:
SELECT `TABLE1`.name
from Table 1
Where `TABLE1`.name LIKE '%$search%'
But for the second part I need your help...
Thank you!
You need to look into using JOIN:
SELECT T.Name, T5.Diagram_Name
FROM Table1 T
JOIN Table2 T2 ON T.FK_TABLE2 = T2.PRIMARY_KEY
JOIN Table3 T3 ON T2.FK_TABLE3 = T3.PRIMARY_KEY
JOIN Table4 T4 ON T3.FK_TABLE4 = T4.PRIMARY_KEY
JOIN Table5 T5 ON T4.FK_TABLE5 = T5.PRIMARY_KEY
WHERE T.Name LIKE '%$search%'
If you want to return the names that don't have matching diagram names, use LEFT JOIN instead.
Good luck.
You are going to need to JOIN the tables using the Primary Key and FK values:
select t1.name, t5.DIAGRAM_NAME
from table1 t1
left join table2 t2
on t1.FK_TABLE2 = t2.PRIMARY_KEY
left join table3 t3
on t2.FK_TABLE2 = t3.PRIMARY_KEY
left join table4 t4
on t3.FK_TABLE3 = t4.PRIMARY_KEY
left join table5 t5
on t4.FK_TABLE4 = t5.PRIMARY_KEY
Where t1.name LIKE '%$search%'
If you need help learning JOIN syntax, here is a great visual explanation of joins.
I used a LEFT JOIN in my example query, which will return all rows from table1 even if there is not a matching row in the remaining tables.
If you know that there is a matching row in all of the tables that you are joining, then you can use an INNER JOIN.

How can we get the value of a column to use as a table name for an inner join?

How can we reference the column value from one table as the reference for a join (see example below)?
SELECT t1.*, t2.*, t3.* FROM term_relationships as t1
INNER JOIN modules as t2 ON t2.module_id = t1.object_id
INNER JOIN t2.nextOfKinTable as t3 ON t3.module_id = t2.module_id;
I thought about using the information_schema but it is too much writing to accomplish something that might be easier by just maintaining a reference to the table you want joined to the current join result, only that I don't know how to make them join this way. Please help :(
Edit:
Essentially what we want is to join table1, table2, and table3 only that the name for table3 is a value stored in table2.
The common column in this case is module_id (object_id)
And the unknown table is t2.nextOfKinTable
Try this out
declare #tabName varchar(1000)
set #tabName = (select top 1 ProductName as tabName from products)
declare #query varchar(8000)
set #query = ' select
p.ProductName as '''+#tabName+'''
from Products p'
---print(#query)
exec (#query)
There's no standard way to do this in SQL. Consider if your t2 table contained 1000 rows, and each row has a distinct nextOfKinTable value. That would result explode the query into a 1002 table join. Not pretty. I'm not even aware of any proprietary syntax that would support it in any products I know of.
If the number of distinct column values are small, you can use LEFT JOINs, but each joined table will receive a different alias (example using 3 tables):
SELECT
t1.*, --TODO: List columns
t2.*, --TODO: List columns
COALESCE(t3.ColumnA,t4.ColumnA,t5.ColumnA) as ColumnA,
COALESCE(t3.ColumnB,t4.ColumnB,t5.ColumnB) as ColumnB
FROM
term_relationships as t1
INNER JOIN
modules as t2
ON
t2.module_id = t1.object_id
LEFT JOIN
Table3 as t3
ON
t3.module_id = t2.module_id AND
t2.nextOfKinTable = 'Table3'
LEFT JOIN
Table4 as t4
ON
t4.module_id = t2.module_id AND
t2.nextOfKinTable = 'Table4'
LEFT JOIN
Table5 as t5
ON
t5.module_id = t2.module_id AND
t2.nextOfKinTable = 'Table5'
You might also want to consider whether these separate tables ought actually to be a single table, with additional column(s) to distinguish the rows. This problem is sometimes referred to as attribute splitting. (See Joe Celko's example of Table splitting in an article from 2009)