I need to make a query like this:
SELECT table1.*, table2.column
FROM table1
LEFT JOIN table2 ON table1.column = table2.column
But it's not working. If I try the same query but replacing the first part -- >
SELECT table1.column, table2.column
FROM table1
LEFT JOIN table2 ON table1.column = table2.column
it works fine. I need to bring all the columns from table1. How can I make it without specifing all of them?
If you have the same column name in table1 and table2, here is the solution for not specifying all the column name of table1 :
SELECT table1.*, table2.column as my_column_name_no_in_table1
FROM table1
LEFT JOIN table2 ON table1.column = table2.column
If the column names of table1 and table2 are all different, you can use :
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2 ON table1.column = table2.column
But as said in the peterm's comment, it is not a good practice in production. Now, do as you want! ;)
Related
I need any help getting data from three tables.
This is my setup:
TableA with two columns: id, name
TableB with three columns: id_a, id_c1, id_c2
TableC with two columns: id, name
I want as result the following table:
TableA.name,TableC.name,TableC.name
where the TableC.name(s) are expansion of id_c1, id_c2 of TableB.
Any idea?
Very thanks
You need to join the 3 tables like this:
select a.name, c1.name, c2.name
from tablea a inner join tableb b
on b.id_a = a.id
inner join tablec c1
on c1.id = b.id_c1
inner join tablec c2
on c2.id = b.id_c2
Maybe instead of inner joins you need left joins, but I can't tell for sure.
I'm trying to get the result into a variable (is it possible to do it as a %rowtype of an existing table? probably not because of conflicting columns) where it would display me all the values where the two refs overlap and the i_ref(which is inputted ) also overlaps with the ref from t1.
select *
into aRow
from table1 t1
where t1.ref = i_ref
and (select * from table2 t2 where t1.ref = t2.ref);
What am I doing wrong with my select?
You can join tables instead of nested subquery:
select t1.*
into aRow
from table1 t1 join table2 t2 on t1.ref = t2.ref
where t1.ref = i_ref
Following is my query:
Select Count(*)
from Table1 s
left join Table2 d
ON s.subjectid = d.subjectid
and s.PROJECTID = d.projectid
and s.SITEName = d.SITENAME
left join Table3 dev
on s.subjectid = dev.subjectid
and s.projectid = dev.projectid
and s.siteid = dev.siteid
Where s.isprod =1
and d.isprod =1
and dev.isprod = 1
and s.projectid =107
-- Output 301 ROWS
This query returns 301 rows. However, if I don't use Table3 then the join returns 2203 rows, as shown in the query below:
Select Count(*)
from Table1 s
left join Table2 d
ON s.subjectid = d.subjectid
and s.PROJECTID = d.projectid
and s.SITEName = d.SITENAME
Where s.isprod =1
and d.isprod =1
and s.projectid =107
-- OutPut 2203 ROWS
By my understanding of left join, all the rows from the left table should remain even if they don't match with the right table. However in this case, the number of rows is reduced from 2203 in query 2 to 301 in query 1. How is that possible?
Please suggest what could be going wrong here. For more clarification:
Table1 with the same where conditions as above has 1300 rows
Table2 with the same where conditions as above has 2203 rows
Table3 with the same where conditions as above has 129 rows
When you have conditions in your where clause that put non-null constraints on the records from the table you have outer joined, you effectively destroy the effect of the outer join, and make it act as an inner join
The solution is to move such constraints into the on clause of the outer join:
Select Count(*)
from Table1 s
left join Table2 d
ON s.subjectid = d.subjectid
and s.PROJECTID = d.projectid
and s.SITEName = d.SITENAME
and d.isprod =1
left join Table3 dev
on s.subjectid = dev.subjectid
and s.projectid = dev.projectid
and s.siteid = dev.siteid
and dev.isprod = 1
Where s.isprod =1
and s.projectid =107
The first SQL has additional "where" clause of "and dev.isprod = 1". Most likely this is reducing the number of rows returned.
I have a table in my database I do a Select all on:
SELECT * FROM TableA;
I want to append a column that is true or false if there's a related column in anther table. I can do it with this:
SELECT *, (SELECT COUNT(Id) > 0 FROM TableB WHERE Id = TableA.Id) FROM TableA;
But I don't want to have to count EVERY row in TableB to work this out as its ineffient. I essentially want an EXISTS check instead of count.
How do I replace the COUNT with EXISTS?
Thanks!
By using a left join
SELECT a.*, b.id is not null as condition_check
FROM TableA a
LEFT JOIN TableB b ON a.Id = b.Id
Ah - just realised the answer to my own question
SELECT *, EXISTS(SELECT Id FROM TableB WHERE Id= TableA.Id) AS DoesExist FROM TableA
SELECT table1.* , table2.Value
FROM table1
INNER JOIN table2
ON table1.id = table2.id
WHERE table2.Label = "Currency"
This is the query. I need to return the values even if Label = currency does not exists.
i.e., I need to return all rows of table1 with unique id. If table2 has currency then the currency value should be taken else empty value should return.
Try using OUTER JOIN like this:
SELECT table1.* , table2.Value
FROM table1
LEFT JOIN table2
ON table1.id = table2.id
AND table2.Label = "Currency"
It sounds like you want something along these lines.
SELECT table1.* , table2.Value
FROM table1
left join table2 on table1.id = table2.id
I'm assuming that table2.value is the currency value you're talking about.
Edit your question, and paste CREATE TABLE statements for more and better answers.
try this
SELECT table1.* , table2.Value FROM table1 left outer join table2 on table1.id = table2.id where table2.Label = "Currency"