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
Related
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
);
There are two table TABLE1 and TABLE2 in which there is a common field ID. I wanted to retrieve values from TABLE2 that doesnot match in TABLE1 based on ID value.
select * from TABLE2 where subject = 1 and ID NOT IN (select ID from TABLE1 where subject = 1)
Sample:
TABLE1 ID SUBJECT 1 1
TABLE2 ID SUBJECT 1 1 2 1
The expected result is 2 and it works fine.
But when TABLE1 is empty or the inner select ID from TABLE1 where subject = 1 returns empty, the whole select statement returns empty.
But the expected result is 1, 2
Is there any way to achieve this ?
Use a left join
select t2.*
from table2 t2
left outer join table1 t1 on t1.id = t2.id and t1.subject = 1
where t2.subject = 1
and t1.id is null
See a good explanation of joins
I think you can use not exists also for this work -
select * from TABLE2 where subject = 1 and NOT exists
(select 1 from TABLE1 where subject = 1 and table1.id = table2.id)
I have these tables and values:
Table1
------------------------
ID | Value
------------------------
2 | asdf
4 | fdsa
5 | aaaa
Table2
------------------------
ID | Value
------------------------
2 | bbbb
4 | bbbb
5 | bbbb
I want to update all the values in Table2 using the values in Table1 with their respective ID's.
Is there a way to do that with a simple SQL query?
Run a select to make sure it is what you want
SELECT t1.value AS NEWVALUEFROMTABLE1,t2.value AS OLDVALUETABLE2,*
FROM Table2 t2
INNER JOIN Table1 t1 on t1.ID = t2.ID
Update
UPDATE Table2
SET Value = t1.Value
FROM Table2 t2
INNER JOIN Table1 t1 on t1.ID = t2.ID
Also, consider using BEGIN TRAN so you can roll it back if needed, but make sure you COMMIT it when you are satisfied.
If you have ids in both tables, the following works:
update table2
set value = (select value from table1 where table1.id = table2.id)
Perhaps a better approach is a join:
update table2
set value = table1.value
from table1
where table1.id = table2.id
Note that this syntax works in SQL Server but may be different in other databases.
You can use alias to improve the query:
UPDATE t1
SET t1.Value = t2.Value
FROM table1 AS t1
INNER JOIN
table2 AS t2
ON t1.ID = t2.ID
None of above answers worked for me in MySQL, the following query worked though:
UPDATE
Table1 t1
JOIN
Table2 t2 ON t1.ID=t2.ID
SET
t1.value =t2.value
WHERE
...
I have used this one on MySQL, MS Access and SQL Server. The id fields are the fields on wich the tables coincide, not necesarily the primary index.
UPDATE DestTable INNER JOIN SourceTable ON DestTable.idField = SourceTable.idField SET DestTable.Field1 = SourceTable.Field1, DestTable.Field2 = SourceTable.Field2...
First select query
Table 1:
ID Value
131 ABC
120 DEF
Second select query
Table 2:
ID
120
131
I want to write a single query which will fetch me combining two tables (the required output)
ID Value
120 DEF
131 ABC
Note: if there is no entry in Table2, return the data from Table1 else combine and return the result.
Any thoughts? Thanks.
SELECT *
FROM table1 LEFT JOIN table2
ON table1.ID = table2.ID
if it can't find the matching record in table2 it will fill table2 columns in the result set with null
SELECT * FROM Table1,Table2 WHERE Table1.ID = Table2.ID
select t1.id, value
from table1 t1, table2 t2
where t1.id = t2.id
something like that:
SELECT *
FROM Table1 t1
LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.ID
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.