Table1 - has constraints with Table2 & Table3
Table2
Table3
Any data which is present in table1 with constraints with table 2 & 3 is valid data.
There are some bogus data somehow entered in table1 by manually turning off the constraint.
I want to collect those data which is present only in table1 without any constraints.
Is there an easy way to get table1 data in mysql which don't have constraint data attached to it?
Thanks.
If I understand your question, you are looking for missing or bogus records in the parent table. I'm going to imagine that the constraint field in table2 and table3 is id and the fk fields in table1 are table2_id and table3_id. If this is the case, you'd query for missing joins:
SELECT t1.id, t1.table2_id, t1.table3_id FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.table2_id
LEFT JOIN table3 t3 ON t3.id = t1.table3_id
WHERE t1.table2_id IS NULL
OR t1.table3_id IS NULL;
Related
I have a MySQL database where there are 2 Tables: Table1, Table2.
Table1 has this structure:
ID, name, description
Table2 has this structure:
TableID, Metadata, Metavalue
Now, I want to delete, from Table1 AND from Table 2 all the rows WHERE Metadata = "Price" AND Metavalue < 200000
This is what I am trying but it does not work:
DELETE Table1, Table2
FROM Table2
INNER JOIN Table2 ON Table2.TableID = Table1.ID
WHERE Table2.metadata="Price" AND Table2.Metavalue < 200000
Error:
Table2: Not unique Table/Alias
Where am I wrong?
Note: These tables are pretty big so I am using JOIN since as far as I have learnt it optimizes the resources.
The following is an example of a credible query:
DELETE Table1
, Table2
FROM Table1
JOIN Table2
ON Table2.TableID = Table1.ID
WHERE Table2.metadata = "Price"
AND Table2.Metavalue < 200000
Note that this kind of thing can perhaps be better handled with foreign key constraints
I have an issue with select command from 2 tables.
So I have table1 with:
table1_id = int pk;
table1_name;
table1_surname;
table1_age;
table1_address;
table1_city;
And table2 with:
table2_id int pk
table1_id int fk references table1.table1_id;
table3_id;
table2_description;
When I write the following select statement, I get ambigous column name table1.table1_name error:
SELECT table2.table2_id, table2.table1_id, table1.table1_name, table2.table2_description
from table1,
table2 inner join
table1
on table2.table1_id = table1.table1_id;
Honestly I do not understand what is wrong about it?
If i understood correctly, you have problem in below line
from table1, table2
In the above code you are using a CROSS JOIN between table2 and table1 which is not required in your case.
Change your query like following.
SELECT table2.table2_id, table2.table1_id, table1.table1_name, table2.table2_description
from table2
inner join table1 on table2.table1_id = table1.table1_id;
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax:
select t2.table2_id, t2.table1_id, t1.table1_name, t2.table2_description
from table1 t1 join
table2 t2
on t2.table1_id = t1.table1_id;
The problem with your query is that you have two references to table1 because of the comma. You have mentioned the table twice. Hence, when you reference the column, the engine doesn't know what you are referring to. Your version is equivalent to:
from table1 cross join
table2 join
table1
on table2.table1_id = table1.table1_id
table1 appears twice, so any reference to it is ambiguous.
You will notice that I also added table aliases to the query. Table aliases make the query easier to write and to read.
Remove table1, just after from ( mixed old type "comma" and modern join syntaxes)
Use like the following :
SELECT t2.table2_id, t2.table1_id, t1.table1_name, t2.table2_description
FROM table2 t2 INNER JOIN table1 t1 ON ( t2.table1_id = t1.table1_id ) ;
SELECT Table1.Filter, Table1.Condition, Combined.Data FROM Table1
LEFT JOIN
(SELECT Key, Data FROM IF(Table1.Filter, Table2, Table3))) AS Combined
ON Table1.Condition = Combined.Key
I want to create a MySQL View that shows all columns of Table1, and a column from either Table2 or Table3 depending on the field on Table1.Filter.
One simple solution is to LEFT JOIN both Table2 and Table3, with NULL on the column that is not applicable. Is there a way to avoid creating 2 columns?
I cannot UNION Table2 and Table3 as they might contain the same Key.
The following should do what you want:
SELECT t1.Filter, t1.Condition,
COALESCE(t2.Data, t3.Data) as Data
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.Filter AND t2.Key = t1.Condition LEFT JOIN
Table3 t3
ON (NOT t1.Filter) AND t3.key = t1.condition;
You cannot have conditionals choosing tables in the FROM. But, you can have conditions in the ON conditions.
I have two tables that have a one-to-many relationship table1 (one), table2 (many).
table1 has (t1_id) as KEY
table2 has (t2_id) as KEY and t2_t1_id as as refference to table1
Now in table1 I need a column (rand_t2_id) that holds an id from table2, I do not really care which one
This is the query I tried
INSERT INTO table1 (rand_t2_id)
SELECT t2_id
FROM table2
WHERE table1.t1_id = table2.t2_t1_id;
There also needs to be a limit build in somewhere if that is needed. I only need one id from table2
No luck here tho, anyone know a fix?
IMO you could try
UPDATE table1 t1 INNER JOIN table2 t2
ON t1.t1_id = t2.t2_t1_id;
SET t1.rand_t2_id = t2.t2_id
I assume you already have a column named rand_t2_id on your table1.
INSERT INTO table1 (t2_t1_id, rand_t2_id)
SELECT t2_id
FROM ( SELECT t2_id, t2_t1_id
FROM table2
ORDER BY RAND()) AS h
GROUP BY t2_t1_id
ON DUPLICATE KEY UPDATE rand_t2_id = VALUES(rand_t2_id)
I suppose that's what you're after?
This query would insert a random t2_id into relevant table1.
i have a problem and at that point no clue or starting point how to fix:
I have three Tables with the following fields (i have limited to the important fields):
table1 table2 table3
idsp idsp did
customer did
What i need is most fields of table1 and table3, table1 is combined to table2 through field idsp and
table2 with table3 on field did.
As i told, i have no starting point... Anyone help?
Thanks,
Sascha
select t1.*, t3.* from
table1 t1
inner join table2 t2
on t1.idsp = t2.idsp
inner join table3 t3
on t2.did = t3.did
Just carry on using joins one after the other.