Need help writing query requiring table joining - mysql

Is anybody able to help me construct a query based on the schema in the attached image? I have been at this all morning but am hopeless with joins and cannot get it to work.
What I would like to be able to do is to select all rows from table 1 where table 4 ID referenced in table 1 is NOT available to the table 5 ID referenced in table 1.
Please let me know if that doesn't make sense, any help would be greatly appreciated!

I'm not completely sure that I ahve understood the question, but does this query fits your needs?
SELECT *
FROM Table1
WHERE Table1.Table4_ID NOT IN ( SELECT t1.Table5_ID
FROM Table1 as t1
)

I eventually figured this out. The query I needed was:
SELECT Table1.Table1_ID
FROM Table1
INNER JOIN Table5
ON Table1.Table5_ID = Table5.Table5_ID
INNER JOIN Table2
ON Table5.Table2_ID = Table2.Table2_ID
INNER JOIN Table3
ON Table2.Table2_ID = Table3.Table2_ID
INNER JOIN Table4
ON Table3.Table3_ID = Table4.Table3_ID
WHERE Table4.Table4_ID <> Table1.Table4_ID
AND Table1.Table5_ID = 5

Related

Can a table be used twice differently in the same query? [duplicate]

How would I self join a table to show the Name, employee number, manager's name of those who are managed by either Blake or Jones?
I'm trying to line it up in the following manner:
SELECT
FROM
INNER JOIN
ON
WHERE
The problem I am having is I have understood MySQL very well up until now, and I cannot seem to grasp the concept of the table joining itself.... any help would be appreciated. Thanks in advance
MySQL self join that joins a table to itself using join
SELECT *
FROM table1 AS t1
INNER JOIN table1 AS t2
ON t1.col_name=t2.col_name
WHERE t1.col_name='xyz'
select t1.name,t1.employee_number,t1.manager_name from table t1 join
table t2 where t1.manager_name = t2. manager_name and t2.manager_name in
('Blake','Jones');

Is there a way of showing names instead of the ID in a SELECT SQL query? [duplicate]

How would I self join a table to show the Name, employee number, manager's name of those who are managed by either Blake or Jones?
I'm trying to line it up in the following manner:
SELECT
FROM
INNER JOIN
ON
WHERE
The problem I am having is I have understood MySQL very well up until now, and I cannot seem to grasp the concept of the table joining itself.... any help would be appreciated. Thanks in advance
MySQL self join that joins a table to itself using join
SELECT *
FROM table1 AS t1
INNER JOIN table1 AS t2
ON t1.col_name=t2.col_name
WHERE t1.col_name='xyz'
select t1.name,t1.employee_number,t1.manager_name from table t1 join
table t2 where t1.manager_name = t2. manager_name and t2.manager_name in
('Blake','Jones');

Joins with a SELECT command

I have been looking and trying to join two tables to get one result but with no luck. (yes a newbie)
Here is the scenario:
Table T1
ID | t1_name | **t1_value**
Table T2
ID | t2_something | **t2_name** | t2_more | t2_etc
What I am trying to do is a SELECT query to get a result to ECHO the t1_value.
This is from t2_name equaling the same as t1_name then return the t1_value to ECHO.
These are based on each indivdual "t2 ID" (100 + ids) having its own result.
Im sure this is an easy solution, and all the reading and research I have done on joins i just cant seem to get my head around it. Maybe over reading....
If you can offer a solution/help, (with how it works so I can learn for next time)
That would be appreciated.
JOIN the two tables with the condition t1.t2_name = t2.t2_name, then in the SELECT clause select whatever you want to select.
Something like:
SELECT
t2.ID,
t1.t1_value,
...
FROM t1
INNER JOIN t2 ON t1.t1_name = t2.t2_name;
You might also need to have a look at the different types of JOIN, see this for more information:
A Visual Explanation of SQL Joins.
Try this query
select t1.t1_value
from t1
inner join t2 on t1.t1_name=t2_name
For more study you can refer INNER JOIN
And Different type of JOIN
A VISUAL REPRESENTATION OF JOIN
Select t1_value from T1, T2 where T1.t1_name = T2.t2_name
SELECT t1_name.t1, t2_name.t2 FROM t1, t2 WHERE t1.t1_name = t2.t2_name
That should work as expected, just to clarify you can also use INNER JOIN syntax as referenced here : http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php
You can achieve by just joining two tables t1 and t2.
SELECT
t1.Name,
t1.value
FROM t1,t2
WHERE t1.t1_name = t2.t2_name

Mysql Inner Join

Can anyone help me please? Inner join query working fine. but query displaying duplicate data. I don't to display duplicate data.
here is my query.
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
Here is output
"COLOR";"456";"456";"Nude"
"COLOR";"456";"456";"Ivory"
"COLOR";"456";"456";"Black"
"COLOR";"456";"456";"Coral"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Coral"
"COLOR";"459";"459";"Nude"
"COLOR";"459";"459";"Ivory"
"SIZE";"460";"460";"Large"
"SIZE";"460";"460";"Medium"
"SIZE";"460";"460";"Small"
"SIZE";"470";"470";"Large"
"SIZE";"470";"470";"Small"
"SIZE";"470";"470";"Medium"
"COLOR";"476";"476";"White"
"COLOR";"476";"476";"Black"
"SIZE";"477";"477";"Small"
But i don't to display duplicate data. for example which is displaying here.
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"460";"60";"Black"
is there any way?? thanks
Maybe you just want to group it by names? You seem to be calling a duplicated data what seems to have different ids...
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
GROUP BY t1.class,t2.option_name

How to get data 3 tables?

can u please show me how to query 3 tables using *? thanks
You need to do a join on the tables in order to get the columns of all of them.
Warning: using * to get all columns is bad practice. You should qualify (name) all the columns you need.
Here is an example:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.key2 = t2.key2
INNER JOIN table3 t3
ON t1.key3 = t3.key3
One way you probably don't like:
SELECT *
FROM table1, table2, table3
You have to give way more information.
This generates the Cartesian product of all the three tables.