Is this the correct way to search in MySQL - mysql

I have two tables. t1 and t2. I want to select the values that are from t1 and occurred in t2. What I wrote is the following:
select col1 from t1, t2
where t1.col1y=t2.col2;
Is this is the right way to search for the value ?

Yes, that works, but it's the old form of writing a join. Use the new form:
select t1.col1
from t1
inner join t2 on t1.col1 = t2.col2

Related

MySQL - is it possible to use aliases for column names in join comparison operators?

Example:
SELECT * FROM T1 JOIN T2 on (T1.id = T2.id as aux)
Where I'm trying to alias T2.id as aux - but that's throwing errors
...
so that the resulting fetched columns would have a column name of aux instead of just id for both o f them.
There's no way to alias columns like this. You could do it in a sub-query, but the proper way to achieve what you want is to explicitly list the projected columns, it's good style anyway. So:
SELECT T1.id, T2.id as aux, T1.col1, T2.col2 [,...] FROM T1 JOIN T2 on T1.id = T2.id

Select value if it appears elsewhere in table

I am using mysql and I'm interested in rows where reciprocals appear in a different row in the table.
Imagine 2 columns, each with letters a through z.
Lets say row1 has a,b, row2 has a,c, and row 3 has c,a. I am interested in the pair a,c because it appears both as c,a and a,c in different rows in the table.
Do I have to use a nested select? Or perhaps an exists clause?
I believe this is what you're after, a self-join:
SELECT t1.*
FROM table1 t1
JOIN table1 t2
ON t1.col1 = t2.col2
AND t1.col2 = t2.col1
Here is a SQL fiddle demo: SQL Fiddle
also use SELECT REVERSE('abc') see http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_reverse with above #spencer7593 said
`SELECT t1.*
FROM table1 t1
JOIN table1 t2
ON t1.col1 = t2.col2
AND t1.col2 = t2.col1`

In MySQL, how to update some columns of existing rows and insert new rows from a reference table?

The following will get me values from a reference table t2 I want to insert to or to update existing tuple with in table t1:
SELECT
id, col1
FROM
t2
LEFT OUTER JOIN
t1
ON
t2.id=t1.id
If a tuple with id already exist in t1, it should be updated with the value selected from t2. If a tuple with id does not exist in t1, (id, col1) should be inserted with other columns set to default values.
How to do this efficiently?
use this two querys:
This will join and filter, giving you the values that exists in both tables, so you just do the update
Update t1 set t1.col1 = t2.col1
from t1 inner join t2 on t1.id = t2.id
This will join and filter, giving you the values that are in t2, but not in t1, so you just do the insert.
insert into t1 select t2.id, t2.col1
from t2 left outer join t1 on t2.id = t1.id where t1.id IS NULL
UPDATE:
as I can see from here MySQL uses another sintax for this. So you query may work with this instead of the query above:
UPDATE t1 temp1
INNER JOIN t2 temp2
ON temp1.id= temp2.id
SET temp1.col1= temp2.col1
but the concepts are the same (just a different syntax)
You won't need the Where, because the INNER JOIN will only use fields that match/join.

Work around temporary table in SQL Server 2008 view

I wrote a query in this format..
Select * INTO #xyz FROM ()
which I later want to use to create a view, as...
CREATE VIEW aaa
AS
Select * INTO #xyz FROM ()
but getting the following errors:
Incorrect syntax near the keyword 'INTO'.
Views or functions are not allowed on temporary tables
Can someone please suggest a workaround this? Is it possible to use temporary tables to create views?
You certainly cannot use a select into in a view. And a termp table is not approraite either. Use a derived table that is the equivalent of the temp table instead. Example:
Suppose you had:
select t1.id, t2.field1 into #temp from table1 t1
join Table2 t2 on t1.id = t2.id
where t2.somefield2 = 'mytest'
select t1.id, t2.field1, t1.field1
from mytable2 t1
join #Temp t2 on t1.id = t2.id
where t2.somefield = 'test'
Then you could use:
select t1.id, t2.field1, t1.field1
from mytable2 t1
join (select t1.id, t2.field1
from table1 t1
join Table2 t2 on t1.id = t2.id
where t2.somefield2 = 'mytest') t2
on t1.id = t2.id
where t2.somefield = 'test'
You could also usea a CTE
Just as the error message says it is not possible to use temp tables.
You should use a permanent table or a CTE which can also be specified in a view.
A CTE could help you out depending on your situation. Describe your problem with some context if you think it suitable after researching what a CTE is. In short a CTE is a query that you can reference multiple times which in the past people used temp tables for.
http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx

which method is better to join mysql tables?

What is difference between these two methods of selecting data from multiple tables. First one does not use JOIN while the second does. Which one is prefered method?
Method 1:
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t2.id
AND t2.id = t3.id
AND t3.id = x
Method 2:
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM `table1` t1
JOIN `table2` t2 ON t1.id = t2.id
JOIN `table3` t3 ON t1.id = t3.id
WHERE t1.id = x
For your simple case, they're equivalent. Even though the 'JOIN' keyword is not present in Method #1, it's still doing joins.
However, method #2 offers the flexibility of allowing extra conditions in the JOIN condition that can't be accomplished via WHERE clauses. Such as when you're doing aliased multi-joins against the same table.
select a.id, b.id, c.id
from sometable A
left join othertable as b on a.id=b.a_id and some_condition_in_othertable
left join othertable as c on a.id=c.a_id and other_condition_in_othertable
Putting the two extra conditions in the whereclause would cause the query to return nothing, as both conditions cannot be true at the same time in the where clause, but are possible in the join.
The methods are apparently identical in performance, it's just new vs old syntax.
I don't think there is much of a difference. You could use the EXPLAIN statement to check if MySQL does anything differently. For this trivial example I doubt it matters.