SQL commands to check ID difference - mysql

I have 2 tables.
How do I know IDs that are in one table and not on the other table? How would I accomplish that?
And then I want to delete all such IDs.

This is pretty simple:
delete from t1
using table1 as t1
left outer join table2 as t2
on t1.id = t2.id
where t2.id is null
It's worth noting that joins are faster than subqueries.

Use this query:
delete from TABLE_A where ID not in (select ID from TABLE_B)

Use this query:
delete from t1
where id not in
(select t2.id from t2)

Related

Is there a more efficient way using mysql to get a list for a given column in an sql result?

I have a query that rows of a table each containing an id.
For each id I want to get multiple values from another table.
The way I would do this is make the first query, then loop through the result making a query for each id.
This could mean making a thousand queries, is there a way I could do this in 1 query.
I think you want group_concat(). Something like this:
select t1.id, group_concat(t2.othercol)
from table1 t1 join
table2 t2
on t1.id = t2.id
group by t1.id;
Or perhaps you just want in:
select t2.*
from table2 t2
where t2.id in (select t1.id from table1 t1);

How to query a date between two other dates in another table

I need to query a date with a value between two other dates that come from another table. Something like this:
select * from table1 where date_table1 BETWEEN (select date1,date2 from table2 where id=1)
How can I do that in SQl?
Just use join or exists. As you have written it:
select t1.*
from table1 t1
where exists (select 1
from table2 t2
where t1.date between t2.date1 and t2.date2
);
I am not sure what you are trying to do but the way you phrased it the question seems incomplete. Based on what you have provided, I think something like this will work:
select t1.*
from table1 t1
inner join table2 t2 on t2.date1 <= t1.date_table1 and t1.date_table1 < t2.date2
where t2.somefield = [somelimitingcondition]
This should work provided each sub-query returns exactly one value
SELECT * from table1 t1
WHERE t1.date_table1
BETWEEN
(SELECT t2.date1 from table2 t2 WHERE t1.id=1)
AND (SELECT tt2.date2 from table2 tt2 WHERE t1.id=1);

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

SQL Server 2008 column in only one table

First a quick explanation: I am actually dealing with four tables and mining data from different places but my problem comes down to this seemingly simple concept and yes I am very new to this...
I have two tables (one and two) that both have ID columns in them. I want to query only the ID columns that are in table two only, not in both. As in..
Select ID
From dbo.one, dbo.two
Where dbo.two != dbo.one
I actually thought this would work but I'm getting odd results. Can anyone help?
SELECT t2.ID
FROM dbo.two t2
WHERE NOT EXISTS(SELECT NULL
FROM dbo.one t1
WHERE t2.ID = t1.ID)
This could also be done with a LEFT JOIN:
SELECT t2.ID
FROM dbo.two t2
LEFT JOIN dbo.one t1
ON t2.ID = t1.ID
WHERE t1.ID IS NULL
Completing the other 2 options after Joe's answer...
SELECT id
FROM dbo.two
EXCEPT
SELECT id
FROM dbo.one
SELECT t2.ID
FROM dbo.two t2
WHERE t2.ID NOT IN (SELECT t1.ID FROM dbo.one t1)
Note: LEFT JOIN will be slower than the other three, which should all give the same plan.
That's because LEFT JOIN is a join followed by a filter, the other 3 are semi-join

mysql extra columns with same name from two tables

Table 1 has columns : entry_id user_id ...
Table 2 has columns : entry_id user_id ...
the user_id entry is not always the same so I would like to extract both of them so I can later on compare them in my script
SELECT * FROM
table1 as t1
INNER JOIN table2 as t2 on t1.entry_id=t2.entry_id
WHERE t1.user_id='%s'
I would like to extract t1.user_id and t2.user_id ...the problem is the result array has only user_id
thank you
Use AS keyword:
SELECT
t1.user_id as t1_user_id
,t2.user_id as t2_user_id
FROM table1 as t1
INNER JOIN table2 as t2
ON t1.entry_id=t2.entry_id
WHERE t1.user_id='%s'
SELECT t1.user_id AS user_id_1, t2.user_id AS user_id2, ...
I think the framework executing the query is at fault for "merging" the two columns here, the result set should have both, but these are stored into an associative array somewhere along the line.
It is a good idea to only request those columns you actually need nonetheless :)
You can effectively use ALIASING (i.e. use the AS keyword).
SELECT t1.user_id as user_id_1, t2.user_id as user_id_2 FROM
table1 as t1
INNER JOIN table2 as t2 on t1.entry_id=t2.entry_id
WHERE t1.user_id='%s'