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.
Related
I would like to know how I can insert new rows in my table 1 of table 2. The idea is that by comparing the two tables if in the second one you do not find the same ID in table 1 this inserts the new data in table 1.
This is the two table and the idea I want to do:
Tabla 1
ID-Name-Surname
1-Oriol-Molina
Tabla 2
ID-Name-Surname
1-Oriol-Molina
2-Ricard-Martin
And the result would be this:
Tabla 1
ID-Name-Surname
1-Oriol-Molina
2-Ricard-Martin
Tabla 2
ID-Name-Surname
1-Oriol-Molina
2-Ricard-Martin
Use the database to enforce data integrity. That is, if you don't want duplicate ids in the table, then declare a unique index/constraint:
create unique index unq_table1_id on table1(id);
Then, in MySQL, you can use on duplicate key update:
insert into table1 (id, name, surname)
select id, name, surname
from table2
on duplicate key update id = values(id);
The final statement is a no-op -- it does nothing except prevent an error.
The advantage of this approach is that the database will ensure that id is unique for any statement that inserts data into the table, not just this one.
You can use INSERT INTO .. SELECT with LEFT JOIN and IS NULL check, to fetch only those rows from Table2 which do no exist in Table1
INSERT INTO Table1 (ID, Name, Surname)
SELECT t2.ID, t2.Name, t2.Surname
FROM Table2 t2
LEFT JOIN Table1 t1 ON t1.ID = t2.ID
WHERE t1.ID IS NULL
You can try using a left join
insert into table1
select id, name, surname from table2 left join table1 on table2.id=table1.id
where table1.id is null
I have a table that i wanna UPDATE with another table so i have something like :
UPDATE table1 JOIN table2 ON table1.id = table2.fk_table1 SET table1.field1 = table2.field1
But i also want to delate all the row from this table who are not in this UPDATE for have something like :
DELETE FROM table1 WHERE id not IN (UPDATE ...)
Their is a way to do that in one optimize sql request or i have to do it in two request?
Thanks
You have to do it in two request as they are two different operation DML operations:
First fire your update statement:
UPDATE table1 JOIN table2 ON table1.id = table2.fk_table1 SET table1.field1 = table2.field1
Then fire your delete request by converting update in select:
DELETE FROM table1 WHERE id not IN (Select...)
Note: Use select with same condition in update command to get the list of records which are updated in first statement.
You can use below query-
DELETE FROM tbl1.* FROM
table1 AS tbl1
LEFT JOIN table2 AS tbl2 ON table1.id = table2.fk_table1
WHERE tbl2.id IS NULL
But it will be slow as per table size and create locking so you should do that first fetch all id from table1 and then delete them.
SELECT tbl1.id FROM
table1 AS tbl1
LEFT JOIN table2 AS tbl2 ON table1.id = table2.fk_table1
WHERE tbl2.id IS NULL
Now delete these ids either put in clause or multiple chunks as per no of records.
DELETE FROM table1 WHERE id IN ();
Consider the following:
QUERY
SELECT * FROM
`table1`,`table2`
WHERE `table1`.`RemoteID` = `table2`.`ID`
AND `table2`.`UserID`=1
How can I change it from a SELECT to DELETE from table1 where these records match? It must only delete from table1, not table2
In less specific terms, I want to delete all records from table1 where they match some criteria of both tables (discretely and relatively)
You can use IN with sub query
DELETE FROM table1
WHERE `table1`.`RemoteID` IN (
SELECT ID
FROM table2
WHERE `table2`.`UserID`=1)
Try this,
Delete
from table1
where Id in
(select table1.Id
from table1 t1, table2 t2
where t1.RemoteID = t2.ID
AND table2.UserID = 1)
I have two tables for images, now I like to pack all images in one table and reference it via foreign key fk_image:
Also:
Table1: ('id_system' , 'logo_image_raw', 'fk_image')
Table2: ('id_image', 'image_raw_data')
How can I copy/move logo_image_raw to Table's 2 'image_raw_data' and reflect its id_image in fk_image?
You Can Do Just like That ..
INSERT INTO table2
SELECT table1.fk_image, table1.logo_image_raw
FROM table1, table2 WHERE table1.fk_image = table2.id_image
Just do INSERT...INTO SELECT
INSERT INTO table2 (id_image, image_raw_data)
SELECT fk_image, logo_image_raw
FROM table1
if the column image_raw_data is null but you have already id_image, you need to join the tables so you can get the specific image for each ID,
UPDATE table2 b
INNER JOIN table1 a
ON a.fk_image = b.id_image
SET b.image_raw_data = a.logo_image_raw
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;