I have 3 columns(id,parent,value) with some data and this query:
SELECT * FROM test WHERE id = (
SELECT parent FROM test WHERE id=2 AND value='value' LIMIT 1
);
The query above works great.
Now how can I delete instead of select that id in one query?
You cannot delete from a table and select from that same table in a subselect.
You can however use that table in a self-join.
DELETE t1 FROM test t1
INNER JOIN test t2 ON (t1.id = t2.parent)
WHERE t2.id = 2 and t2.value = 'value'
You cannot use limit not order by in a joined delete statement.
It's either the join or the limit/order by, take your pick.
See: http://dev.mysql.com/doc/refman/5.5/en/delete.html
DELETE FROM test WHERE id = ( select * from (
SELECT parent FROM test WHERE id=2 AND value='value' LIMIT 1 ) as t
)
Related
I need to do selection from mysql 5.7.22 in one query.
select id from t1 where type_id=(select type_id from t2 where id=1 limit 1) and id not in
(select obj_id from t2
where
type_id = (select type_id from t2 where id=1 limit 1)
and
type2_id = (select type2_id from t2 where id=1 limit
...
)
I have some duplicate subquerys in where clause (it's only part of the query, this subquery duplicates many times)
'(select type_id from t2 where id=1 limit 1)'
Can I some how figure it out in one place, to reduce verbose.
So I want to select once
select type_id, type2_id from t2 where id=1 limit 1
and make type_id, type2_id available in all query context.
I know mysql 8.0 has WITH syntax, but I am using 5.7.22
I want to do this in one query without transactions.
It's hard to give you complete advice without seeing your more of your query. But you have some choices.
You could try creating a view as follows then using it.
CREATE VIEW selector
AS SELECT MAX(type_id) type_id, MAX(obj_id) obj_id
FROM t2
WHERE id = 1
It looks possible that the t2 query returns multiple rows. This view deals with that by using MAX() instead of LIMIT 1. But if t2.id is a primary key, then all you need is
CREATE VIEW selector
AS SELECT type_id, obj_id
FROM t2
WHERE id = 1
Then you can use the view in your query.
For example
SELECT id
FROM t1
WHERE type_id = (SELECT type_id FROM selector)
AND obj_id <> (SELECT obj_id FROM selector)
Or you could figure out how to use join operations rather than subqueries.
SELECT id
FROM t1
JOIN selector ON t1.type_id = selector.type_id AND t1.obj_id <> selector.obj_id
try this
select id from t1 ,(select type_id from t2 where id=1 limit 1) t where type_id=t.type_id and id not in
(select obj_id from t2
where
type_id = t.type_id
and
type2_id = t.type_id
...
)
I need to update a table consisting of million rows
there are two tables table1 and table2
SELECT ID
FROM (
select ID from table1 where<condition>
) as result1
INNER JOIN table2 ON result1.field=table2.field
GROUPBY table2.field
HAVING <condtion>
and on this #resultset1 ID, I have to update table 1
UPDATE table1
SET x=true
where ID EXISTS IN (#resultset1)
there are millions of rows in both table. how do i do it?
And Can anyone say whats wrong with this, i am trying some alternative over join
UPDATE table1 t1
SET x=true
WHERE <condition> AND EXISTS(
SELECT* FROM (
SELECT *
FROM table2 t2
WHERE t2.field = t1.field
) AS result
WHERE<condition on resultset field>
);
Output the results of the first query to a file.
Convert the output to update statements, one update per id (using sed or whatever)
Split the statements into separate files, maybe a 1000 per file (using split or whatever)
Run each file as an sql script with a pause (eg 10 seconds) between each execution (to allow logs to update etc and spread the load), using a simple runner script the loops over the files
Why can't you simply do like below. Though I don't really see a need of group by and having provided if this is your actual query.
UPDATE table1
SET x=true
where ID IN (
SELECT ID
FROM (
select ID from table1 where<condition>
) as result1
INNER JOIN table2 ON result1.field=table2.field
GROUPBY table2.field
HAVING <condtion>
)
Another good option would be JOIN both result set and do the UPDATE like
UPDATE table1 t1
JOIN
(
SELECT ID
FROM (
select ID from table1 where<condition>
) as result1
INNER JOIN table2 ON result1.field=table2.field
GROUPBY table2.field
HAVING <condtion>
) X ON t1.ID = X.ID
SET t1.x=true
EDIT:
You can as well store the result of fist query in a temporary table (As already suggested by a_horse_with_no_name) and then do the UPDATE using a JOIN with the temporary table. Somethig like below
create temporary table idtemp(ID INT);
insert into idtemp
SELECT ID
FROM (
select ID from table1 where<condition>
) result1
INNER JOIN table2 ON result1.field=table2.field
GROUPBY table2.field
HAVING <condtion>
Finally do the update like
UPDATE table1 t1
JOIN idtemp it ON t1.ID = it.ID
SET t1.x=true
How do I display in SELECT results 1 if a variable exits in other table and 0 if not? Is it possible or I have to JOIN? And in case it is possible only by JOIN, what if my SELECT is really complicated and I want to LIMIT it before JOINING?
Lets say that table 1 and table 2 contain column named pid. Would like to select * from table 1 , limit it to 100 results (limit 100), and add one column to results determinating if a pid of a result in table 1 is in table 2.
Try
SELECT t1.*, (t2.pid IS NOT NULL) exists_in_table2
FROM
(
SELECT *
FROM table1
ORDER BY pid
LIMIT 100
) t1 LEFT JOIN table2 t2
ON t1.pid = t2.pid
Here is SQLFiddle demo
Try #peterm solution and if you want to check more conditions you can use CASE statement
SELECT t1.*,
case when t2.pid IS NULL then 0 else 1 end as exists_in_table2
FROM
(
SELECT *
FROM table1
ORDER BY pid
LIMIT 100
) t1 LEFT JOIN table2 t2
ON t1.pid = t2.pid
This is the same as #peterm told, I just changed the checking with CASE statement and the SQLFIDDLE
I'm not a SQL guy, and I've just tried every JOIN MySQL offers.
I primarily need data from view1 using WHERE, ORDER BY, LIMIT with offset (the data is incorrect without those conditions.)
view1 is not limited by view2
Data also needs to be pulled from view2, but view2 has its' own WHERE (or whatever's best) condition
view2 may not have corresponding data, but it shouldn't be grabbed without being linked to view1 (NULLs are just fine!)
I'll just give you the two SELECTs that work for me rather than the hashes I've come up with.
For view1:
SELECT * FROM view1
WHERE column1 IS NULL OR column1 = 1
ORDER BY dateColumn DESC LIMIT index1, count1;
(index1 and count1 are INs i use for a stored proc)
For view2:
SELECT * FROM table2
WHERE column1 = ? AND table1_id IN (
SELECT id FROM view1
WHERE column1 IS NULL OR column1 = 1
ORDER BY dateColumn DESC LIMIT index1, count1
)
GROUP BY table1_id
(? is any arbitrary value i put in)
I'm at wits end. I have no idea how to fuse these two.
Specifics
Please note that the IN for view2 is almost identical to the view1 query. Thanks!
You can write a subquery for table2 and add WHERE clause there, also you can add WHERE clause at the ent of the query -
SELECT * FROM table2 t2
JOIN (
SELECT id FROM table1
WHERE column1 IS NULL OR column1 = 1
ORDER BY dateColumn DESC
LIMIT index1, count1
) t1
ON t1.id = t2.table1_id
WHERE
t2.column1 = ?
GROUP BY
t2.table1_id
In this example I used JOIN instead of WHERE IN condition.
How about this query:
Select t1.id
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.table1_id
WHERE t1.column1 IS NULL OR t1.column1 = 1
i think it should help you
Select TableName1.id
FROM TableName1
JOIN TableName2 ON TableName1.id = TableName2.id
WHERE TableName1.column1 IS NULL OR TableName1.column1 = 1
I have two different tables
in first one (t1) I have
Table [t1]
id Product_URL
.
Table [t2]
id Product_id Product_URL
I would like to UPDATE ALL product_id field (from t2) to the id of the first
WHERE t1.product_url = t2.product_url
can I do that In one query?
UPDATE t2
JOIN (
SELECT t2_2.id, t1.id as new_id
FROM
t2 t2_2 JOIN
t1 ON t2_2.product_url = t1.product_url AND t2_2.product_id <> t1.id
ORDER BY t2_2.id
LIMIT 5000
) sub ON t2.id = sub.id
SET id = sub.new_id;
EDIT: It looks like mutli-table updates do not play well with LIMIT and ORDER BY, but here's another query that accomplishes the same thing...