Flushing out table according to results from another table - mysql

I have two interconnected tables: one stores some general result (table1), the other one details N rows for each result (table2).
Each record in table2 has a field recording the "auto_id" of the table1 row of reference (field is called "ref_id")
I intentionally deleted some records from the table1, but I left the referenced rows in table2.
How can I find all rows with invalid "ref_id"s in table2 that link to a no more existing "auto_id" in table1?
I was thinking something like
SELECT *
FROM table2
WHERE NOT EXISTS(
SELECT auto_id
FROM 'table1'
WHERE 'table2.res_id' = auto_id
)
but there-s obviously some error.
Thanks for the help!

You are using the wrong quotes. Single-quotes (apostrophes, or ASCII 39) are for literal strings. The easiest solution would be to remove the quotes:
SELECT *
FROM table2
WHERE NOT EXISTS(
SELECT auto_id
FROM table1
WHERE table2.res_id = auto_id
);
If you want to quote identifiers in MySQL, use backticks (ASCII 96).
SELECT *
FROM `table2`
WHERE NOT EXISTS(
SELECT auto_id
FROM `table1`
WHERE `table2`.`res_id` = `auto_id`
);

The cleanest way IMHO is an outer join filtering for missing rows:
SELECT t2.*
FROM table2
LEFT JOIN table1 t1 ON t1.auto_id = t2.res_id
WHERE t1.auto_id IS NULL
This works because missed left joins have all nulls in their columns and where clause conditions are applied after the join is made - the IS NULL condition means the only rows returned are those that don't have a matching row in the other table.
As well as being the most efficient (assuming an index on table1.auto_id), it also makes for a briefer query than a NOT IN (...) query.

MySQL JOIN is the best option for you.
Try this:
SELECT *
FROM table2 T2
JOIN table1 T1
ON T2.`res_id` != T1.`auto_id`

Related

Specify table-column on "in" operator

i would like to know if there is any shortcut to specify the column where IN have to check for matches.
Example:
Instead of this:
select *
from table1
where id in(
select column
from table2
)
something like this:
select *
from table1
where id in table2.column
I know the existence of TABLE for IN, ANY, SOME to specify a table, but it works only if the table specified is composed by just 1 column
EDIT: using join is not an option, because the real use i was looking for is on a NOT IN operator, and also JOIN create duplicates sometimes like in a one to many relation
There is no shortcut like that in SQL. Let me explain why.
In a query, all table references need to be made in the FROM clause. Hence, you cannot simply refer to table2.col unless table2 has been defined in the FROM clause. table2 is actually an alias, which defaults to the table name.
From a performance perspective, I would recommend exists:
select t1.*
from table1 t1
where exists (select column
from table2 t2
where t2.column = t1.id
)
In particular, this can take advantage of an index on table2(column) and has the same semantics as in.
Using a JOIN is a bit shorter. At least it does not require a subquery or another SELECT ... FROM.
SELECT table1.*
FROM table1
JOIN table2 ON table1.id = table2.column
Although this simple example is an inner join, not a semi-join. An inner join is different because it produces one row per matched row in table2. A semi-join only produces one row for each row in table1, even if it matches multiple rows in table2.
If you want to simulate a semi-join, use DISTINCT to reduce the result to one row per row of table1:
SELECT DISTINCT table1.*
FROM table1
JOIN table2 ON table1.id = table2.column
If you want to check for something like NOT EXISTS, use an exclusion join:
SELECT table1.*
FROM table1
LEFT OUTER JOIN table2 ON table1.id = table2.column
WHERE table2.column IS NULL
No need to use DISTINCT on the outer join example. There will be no row duplication from the join, because it can only "match no rows" once.

Select everything from joined tables

I have two joined tables:
SELECT table1.*, table2.* FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.t1_id
The question: In query results, id will always be taken from secondary table defined in SELECT statement?
For example:
if I use select t1.*, t2.* - in results id will be t2.id
if I use select t2.*, t1.* - id will be t1.id.
Is this good practice to use 'merged' result, or should I avoid ambiguity, and always define columns strictly?
No, the sql query will return all columns with the same name from all tables, not just the last one, unless you use a natural join (table1 inner join table2 using(column)).
If you use some kind of a component that stores the results in associative arrays, then these components usually use only the field names as key, therefore they return only the last column's value from those that have the same name.
However, it is a good practive to use an alias if you want to return more than 1 column that has the same name in the database.
My suggestion is to use tablename with alias and get to use like this. It would be best practice to run query.Mention your column names even though it has many columns. You can order your display.
SELECT t1.columnName1, t2.columnName2 FROM tablename1 t1 INNER JOIN tablename2 t2 ON t1.id = t2.id

I wanted to know the command to check if all the values in one field of a table is present in another table under a different field name

I have 2 tables. I want to find out whether the values present in the first table is there in another table with a different field name.
Here is how it looks,
Table1
BillNo
43529179
43256787
35425676
25467778
24354758
45754748
Table2
BNo
113104808
25426577
268579679
2542135464
252525232
235263663
I have 137 records in table1 that needs to be checked against table2.
Instead of doing it one by one using the following command,
Select * from Table2 where BNo = '43529179';
This gives the result for just the mentioned value. Is there a way to check for all the values in a single query?
Thanks!
You can use a sub-select to compare against:
Select * from Table2 where BNo IN (SELECT BillNo FROM Table1);
That will "evalaute" to something like Select * from Table2 where BNo IN (113104808, 25426577, 268579679, 2542135464, 252525232, ...);
Join the tables, and check how many matching records there are:
select
count(*) as Matches
from
Table1 as t1
inner join Table2 as t2 on t2.BNo = t1.BillNo
You can also use a left join to pick out the records in table 1 that has no matching record in table 2:
select
t1.BillNo
from
Table1 as t1
left join Table2 as t2 on t2.BNo = t1.BillNo
where
t2.BNo is null

MYSQL count first table ids where value is found in second table

I have two tables,
table1 and table2 both tables has these columns
id, name, rel_id
now i would like to have a query to count ids of the table1 where name from table2 is equals to john and table1 rel_id equals to table2 rel_id.
so something like this (this is not correct that's why i need help to make it work).
Select count(ids) from table1
where table2.name="john"
and table1.rel_id=table2.rel_id
Well, one way is to use a join:
Select count(t1.id)
from table1 t1 join
table2 t2
on t1.rel_id = t2.rel_id
where t2.name = 'john';
Note that this uses table aliases to distinguish all the columns in each table. Because the tables have the same columns, you need to identify the table for each column. Also, the I changed the string constant to use single quotes rather than double quotes.
You need to look into joins so :
select count(ids)
from table1 join table2 on table1.rel_id=table2.rel_i
where table2.name="john"
A short intro from W3C schools: http://www.w3schools.com/sql/sql_join.asp
The full MySQL URL for more reference http://dev.mysql.com/doc/refman/5.0/en/join.html

CREATE TABLE... SELECT combining two MySQL tables and NULL values

I'm using this to join two tables (leaving out some of the fields here). All of the fields in the original tables are defined as VARCHAR, NOT NULL, with default ''.
CREATE TABLE newtable
SELECT
table1.field1,
table1.field2,
...
table2.field1,
table2.field2,
...
FROM
table1
LEFT JOIN table2 ON table1.id = table2.id
The join itself is working, however, table 1 has some rows which are not in table 2. The fields from table 2 are being added as NULL for those entries, when I need them to be empty strings. Is there a way to do that when creating the table?
IFNULL(table1.field1,'') AS table1_field1,
IFNULL leaves the value if it is not null otherwise it replaces it with the second parameter, empty string in your case
if you want the columns from table2 to be '' ..put an nvl ...this works in oracle
CREATE TABLE newtable as
SELECT
table1.field1,
table1.field2,
...
nvl(table2.field1,''),
nvl(table2.field2,''),
...
FROM
table1
LEFT JOIN table2 ON table1.id = table2.id