Joining to Same Column but Getting No Rows - sql

I have two tables.
Table1
ID Text
Table2
ID ParentID Text
I am trying to join to the same table twice using 2 different columns. I want all the rows where Table1.ID = Table2.ID as well as all the rows where Table1.ID = Table2.ParentID. I've tried the following but it returns no rows. When I run the script below in two different select statements each with one of the joins only, I get the result I want but not in the same select statement. Any ideas on what I'm doing wrong?
SELECT *
FROM Table 1
JOIN Table2 2
on 2.ID = Table1.ID
JOIN Table2 22
on 22.ParentID = Table1.ID

You're only getting the records where Table 1 is both a Parent ID and an ID... maybe you want:
SELECT
* FROM Table 1
JOIN Table2 2 on 2.ID = Table1.ID
UNION
SELECT
* FROM Table 1
JOIN Table2 22 on 22.ParentID = Table1.ID
Or potentially UNION ALL?

You could try this query:
SELECT
*
FROM
TABLE1,
TABLE2
WHERE
TABLE1.ID = TABLE2.ID
OR
TABLE1.ID = TABLE2.ParentID

Your aliasing is a little off.
Give this a go:
SELECT *
FROM Table 1
JOIN Table2 2
on 2.ID = 1.ID
JOIN Table2 22
on 22.ParentID = 1.ID
You may still get an error on the SELECT * by not having an alias there... I suggest doing SELECT 1.*, or even doing SELECT 1.myColumn, 1.myColumn2 etc...

Related

QUERYING columns from 3 tables using SQL

I have 3 tables, TABLE1, TABLE2 and TABLE3.
TABLE1 have ID and TYPE as columns
TABLE2 have ID, DATE and VALUE as columns
TABLE3 have ID and DESC
Basically TABLE1 is just used as a mapping table. It just tells what type of thing a certain ID is with the TYPE column. We have 1-3 as TYPE.
TABLE2 contains transaction dates per ID. So it's possible to have multiple same ID in TABLE2 but with different DATE. VALUE is just how much the transaction was that DATE for that ID
TABLE3 is like TABLE1 but with a different column DESC.
So my question is how can I query all IDs that are TYPE = 1 (TABLE1) that happened on DATE = 5/27/2018 (TABLE2). The query would contain the columns ID, DATE, VALUE and DESC (TABLE3)
My approach in this one is to use JOIN
SELECT TABLE1.ID, TABLE2.DATE, TALBE2.VALUE
FROM TALBE2
INNER JOIN TABLE1 ON TABLE1.ID = TABLE2.ID
WHERE TABLE1.TYPE = 1 AND TABLE2.DATE = '5/27/2018';
My problem here is that it doesn't work and it doesn't include TABLE3.DESC yet.
I hope you are not using the exact SQL statement as given. It has 2 TALBE-TABLE mix ups.
The query should be something like:
SELECT TABLE1.ID, TABLE2.DATE, TABLE2.VALUE, TABLE3.DESC
FROM TABLE2
LEFT JOIN TABLE1 ON TABLE2.ID = TABLE1.ID
LEFT JOIN TABLE3 ON TABLE2.ID = TABLE3.ID
WHERE TABLE1.TYPE = 1 AND TABLE2.DATE = '5/27/2018'

SQL Query not equal rows

There are two tables table1 and table2 i want to join these tables and expect to retrieve rows which are not equal to table1 as there are rows which are in the both tables.
Table1
ID NAME Dept
1 B Finance
2 R HR
3 B CDU
Table2
ID PASSPORT
1 Yes
2 No
it is not working
SELECT table1.ID, table1.NAME, table1.Dept FROM TABLE1 INNER JOIN TABLE2 ON
TABLE1.ID != TABLE2.ID
Expected Result
ID NAME DEPT
3 B CDU
Use a left join to and where clause to grab the values that are only in table1
select
table1.id,
table1.name,
table1.Dept
from
table1
left join table2 on table2.id = table1.id
where
table2.id is NULL;
Output for the above Query:
A left join will insert a null value if the record from the second table cannot be found. So add in the where clause to grab all results that could not be found in the second table.
Here is a better description of using a left join https://www.w3schools.com/sql/sql_join_left.asp
Try this query --
SELECT ID
,NAME
,Dept
FROM TABLE1
WHERE ID NOT IN (
SELECT DISTINCT ID
FROM Table2
WHERE ID IS NOT NULL
);

Many-To-Many - get all records from related record

This is my many-to-many table:
Table3:
ID_TABLE3
ID_TABLE1_FK
ID_TABLE2_FK
Some_Field
Now what I want is to do a select of all records from TABLE2 where ID_TABLE1_FK in TABLE3 = 3. This is my query, and It returns all records, but It adds all fields of TABLE3 at end - WHICH IS NOT DESIRED !! :
SELECT * from TABLE2
JOIN TABLE3 ON TABLE3.ID_TABLE2_FK = TABLE2.ID_TABLE2
WHERE TABLE3.ID_TABLE1_FK= 3
So where am I wrong ?
Just use a regular JOIN and select the columns you really want;
SELECT t2.*
FROM TABLE2 t2 JOIN
TABLE3 t3
ON t3.ID_TABLE2_FK = t2.ID_TABLE2
WHERE t3.ID_TABLE1_FK = 3;
This could conceivably produce duplicates (if they are in TABLE3). So, you might be better off with:
SELECT t2.*
FROM TABLE2 t2
WHERE EXISTS (SELECT 1
FROM TABLE3 t3
WHERE t3.ID_TABLE2_FK = t2.ID_TABLE2 AND t3.ID_TABLE1_FK = 3
);

Trying to get value from other table using any in SQL

My query is:
SELECT *
FROM table1
RIGHT JOIN table2 on table1.ID = any(table2.parents)
WHERE table1.ID = 1
My first table is:
ID | desc
123 | Data123
231 | Data231
My second table is:
ID | parents
1 | {123,231}
2 | {123}
But this query isn't working. I am trying to get the data from both 123 and 231, how do I fix this?
any(table1.parents)
parents doesn't exists in table1.
Try to change places
SELECT *
FROM table1
RIGHT JOIN table2 on table1.ID = any(table2.parents)
WHERE table2.ID = 1
You didn't specify what exactly you want, but if you just want to find rows in table1 where the IDs are present in table2.parents, the following should do it:
select t1.*
from table1 t1
where exists (select *
from table2 t2
where t1.id = any(t2.parents))
order by t1.id;
If you want information from table2 as well you need a join (and I don't think you want an outer join):
select *
from table1 t1
join table2 t2 on t1.id = any(t2.parents)
order by t1.id
Due to the nature of a join that would return the row (123,Data123) twice - don't know if you want that or not.
Example: http://rextester.com/PUOL76353

mysql join two tables

I have a problem joining two tables:
table1
id name
1 aaa
2 bbb
3 ccc
table2
id table1_id name
1 1 x1
2 1 x2
3 2 s1
table1 is the main table, table2 contains attributes.
I need to join and search both tables, but display distinct results from first table.
When using JOIN I get multiple results from table2.
The scenario is I need to search main table TABLE1 and ALL ATTRIBUTES in TABLE2 and return if found
select distinct(name) from table1 inner join table2 on table1.id = table2.table1_id where table2.name = x2;
Should do the trick.
If you need entries which exists in both tables:
SELECT * from Table1 t1
WHERE YourConditionsHere
AND EXISTS (SELECT 1 from Table2 t2
WHERE t1.Id = t2.Table1_id
AND YourConditionsHere)
if you need entries from Table1 for which does not exists enteries in Table2
SELECT * from Table1 t1
LEFT JOIN
(SELECT * from Table2
WHERE YourConditionsHere
) t2
ON (t1.Id = t2.Table1_id)
WHERE YourConditionsHereForTable1
another option
select * from table1 t1 where t1.id in (select table1_id from table2 t2 where t2.name = "x1");
it's probably best to check query plains (i.e. EXPLAIN) for all suggested queries and check the one that performs best for your exact scenario.