What is the meaning of "*=" in TSQL syntax? - tsql

I see the following syntax in a Sybase TSQL script I have to modify:
table1.Column1 *= table2.Column1
What does the *= portion of the syntax do?

That is the old way of writing a LEFT OUTER JOIN, =* is a RIGHT OUTER JOIN
These two are the same
New ANSI JOIN
SELECT * FROM Test2 t2
LEFT JOIN Test1 t1
ON t1.id = t2.id
Old JOIN
SELECT * FROM Test1 t1,Test2 t2
WHERE t1.id =* t2.id

Related

Postgresql query many to many relationship with left join and use where clouse not show data

I have three table, table_1 (id), table_2(id), table_pivot(tbl1_id, tbl2_id). When I use query like this:
select *
from public.table_1 t1
left join public.table_pivot tp on tp.tbl1_id = t1.id and tp.user_id = 1
left join public.table_2 t2 on t2.id = tp.tbl2_id;
Data from table_1 is show. But when I use where condition. Like this:
select *
from public.table_1 t1
left join public.table_pivot tp on tp.tbl1_id = t1.id
left join public.table_2 t2 on t2.id = tp.tbl2_id
where tp.user_id = 1;
Data from table_1 not show.
I hope advance can help explain why?
Thanks.
Including a where clause on an outer joined table, effectively converts the join into an inner join. If there are no matching values in table_pivot, then
you will get no results at all. This is standard sql.

Select * from to_tsvector - joining tables

I want to set the to_tsvector language (e.g.: 'French') so it uses the proper dictionary when rendering the FTS vector.
Table messages has a locale_id column, which is on the locales table. Which I then need to join the locales table to the languages table on locale_id to get the actual language name. But I'm getting an ambiguous ; error:
select * from to_tsvector(t3.language, t1.message)
inner join message as t1
inner join locales as t2 on (t1.locale_id = t2.id)
inner join languages as t3 on (t2.language_id = t3.id);
ERROR: syntax error at or near ";" LINE 1: ....id)
inner join languages as t3 on (t2.language_id = t3.id);
newer code:
select to_tsvector(t3.language, t1.message)
from message as t1
inner join locales as t2 on (t1.locale_id = t2.id)
inner join languages as t3 on (t2.language_id = t3.id);
original fix:
select * from to_tsvector(t3.language, t1.message)
inner join message as t1 on 1 = 1 /* an ON join criteria is mandatory here even if you are doing an implicit cross join */
inner join locales as t2 on (t1.locale_id = t2.id)
inner join languages as t3 on (t2.language_id = t3.id);

Left join filtering only works on where clause

Can someone explain this to me?
I have two huge tables on which I wish to LEFT JOIN, but I think it would be more efficient to filter at the 'on' rather than the 'where'.
Select * from Table1 t1 left join Table2 t2 on t1.Id = t2.Id and t1.Enabled = 1
Rather than
Select * from Table1 t1 left join Table2 t2 on t1.Id = t2.Id where t1.Enabled = 1
The above results are not the same. No matter what I put in as filtering, it stays unaffected:
Select * from Table1 t1 left join Table2 t2 on t1.Id = t2.Id and t1.Enabled = 1
yields exactly the same results as
Select * from Table1 t1 left join Table2 t2 on t1.Id = t2.Id and t1.Enabled = 0
Also, Table1 might only have a few records where Enabled = 1, so it would be inefficient to join millions of records first and then filter to find only 10.
I can do a sub select first, but I somehow I feel it is not the right way:
Select * from (Select * from Table1 where Enabled = 1) a left join Table2 t2 on a.Id = t2.Id
Types of Joins
Left outer join All rows from the first-named table (the "left"
table, which appears leftmost in the JOIN clause) are included.
Unmatched rows in the right table do not appear.
When you move the condition to the where then t1.Enabled = 1 is applied
Do you have actual performance problems with the first query?

SQL: Joining tables using wildcards

When I join 2 tables
select t1.col1, t1.col2, t2.col3
from t1
left join t2
on t1.col2=t2.col3
Then I don't get any duplicate rows. However, when I try joining
tables using wildcards:
select t1.col1, t1.col2, t2.col3
from t1
left join t2
on t1.col2 like '%'||t2.col3
Then I'll get duplicate values. I saw this post that I think is
getting me somewhere but I couldn't really understand the solution.
Joining 2 Tables using a wildcard
It says I can use exists to get rid of duplicate values. I also
don't really understand his query. Here is the query in the other
post:
select *
from tableA a
where exists (select 1 from tableB b where a.id like '%' + b.id + '%');
What does the select 1 from tableB do?
I'm using PostgreSQL
This is what I've tried even though I don't understand it and still gives me duplicates:
select t1.col1,t1.col2,t2.col3
from t1
left join t2
on t1.col2 like '%'||t2.col3
where exists (select 1 from t2 where t1.col2 like '%'||t2.col3)
Use the DISTINCT clause:
SELECT DISTINCT t1.col1, t1.col2, t2.col3
FROM t1
LEFT JOIN t2 ON t1.col2 LIKE '%'||t2.col3;

left join from a group of tables to another group of tables in postgresql

I have some tables say t1 , t2 , t3.
I need to implement something like this in postgresql.
select * from (t1 , t2) left join t3
where t1.some_column = t3.some_column;
But postgresql complains
ERROR: syntax error at or near "," SQL state: 42601 Character: 77
You can't use from (t1,t2), you have to join them in some way.
Try something like this:
select * from t1
inner join t2 on t1.someColumn=t2.someColumn
left join t3 on t1.some_column = t3.some_column;