I'd like to select from one table and use the results of that select to update another table, but only based on certain conditions. Is this possible with a 1-time SQL query?
Yes it is.
UPDATE
tableToUpdate AS ttu
[LEFT|RIGHT|INNER] JOIN
otherTable AS ot
ON
joinCondition
SET
ttu.field = ot.field
WHERE
conditionsToBeMet
AS otherTable you can just use the SELECT query that you use to fetch your resultset.
Related
Some SELECT statements stores in table as a field. I need to write SELECT statement that joins with some SELECT that returns SELECT.
For example:
SELECT *
FROM table1
JOIN (SELECT t_select FROM table2 WHERE = 'some_condition')
Last SELECT SELECT t_select FROM table2 returns some SELECT statement as text.
I need to join table1 with the result of the query that stores in t_select
Do I understand? Basically, you want to "evaluate" the SELECT that is stored in the table? That seems like a really poor design to me.
If you really need to do this, you'll need to pull the SELECT statement out yourself, and send it as a second query. You can't do this in pure MySQL.
Do you just want a subquery?
SELECT *
FROM table1 t1 JOIN
(SELECT t2.* FROM table2 t2 WHERE = 'some_condition') t2
on t1.<somecol> = t2.<someothercol>;
All in all you can't execute the query that is stored in the table withing another query. You will have to retrieve the query first, prepare it, and then execute it. Have a look at execute immediate :
http://dev.mysql.com/worklog/task/?id=2793
http://www.postgresql.org/docs/9.1/static/ecpg-sql-execute-immediate.html
Storing sql statements in a table is not very common, and there's usually better ways to do it.
I have something like this:
SELECT FROM table
WHERE field NOT IN (
SELECT FROM other_table ... //SECOND QUERY
)
Problem: I don't want SECOND QUERY be executed for each row of table. Can I save QUERY 2 result to some variable?
You can check if the query will run for every row by running EXPLAIN and looking if it its a DEPENDENT SUBQUERY or SUBQUERY. Dependent will run for each row.
Based on that you want to 'save it to a variable' I'm guessing it will not be dependent.
If you want to save a resultset to a 'variable' you need to use a temp table. In this case it will not be necessary.
A LEFT NULL JOIN may be a lot faster for you.
SELECT *
FROM table AS t
LEFT JOIN other_table AS ot ON ot.field = t.field
WHERE ot.field IS NULL
For an assignment, I have to include all of these keywords in one big MySQL statement: select, from, where, group by, order by, inner join, insert, update, delete.
Obviously all but the last 3 are easy to include in 1 statement.
However, I'm having a problem with using union on 2 SQL statements (one with select, etc. and the other with insert).
For example, I have:
SELECT * FROM Database
INNER JOIN (O_Database INNER JOIN ...)
ON ...
GROUP BY ...
ORDER BY ...
UNION
INSERT INTO Database (...) VALUES (...)
But I run into errors with using UNION this way. Is there a simple statement that includes all of these keywords?
I see no solution to put DELETE in there. You can build a select statement with all the stuff required and use this in an INSERT ON DUPLICATE KEY UPDATE Statement. But DELETE? No. In Oracle this would be possible by using the MERGE statement, but this is not available in MySQL. So I guess, there is no solution to the task given.
INSERT INTO ...
SELECT ... -- using from, where, group by, order by, and inner join here
ON DUPLICATE KEY
UPDATE ...
Well, you can do something like
INSERT INTO A
SELECT whatever FROM B;
or
UPDATE A
INNER JOIN (SELECT * FROM B ) C ON A.id = C.id
SET A.whatever = C.whatever;
or
DELETE
FROM A WHERE A.whatever IN (SELECT whatever FROM B);
But you can not combine DELETE and UPDATE or UPDATE and INSERT. Just one of the operations with a SELECT.
Hi i have this query but its giving me an error of Operand should contain 1 column(s) not sure why?
Select *,
(Select *
FROM InstrumentModel
WHERE InstrumentModel.InstrumentModelID=Instrument.InstrumentModelID)
FROM Instrument
according to your query you wanted to get data from instrument and instrumentModel table and in your case its expecting "from table name " after your select * .when the subselect query runs to get its result its not finding table instrument.InstrumentModelId inorder to fetch result from both the table by matching you can use join .or you can also select perticuler fields by tableName.fieldName and in where condition use your condition.
like :
select Instrument.x,InstrumentModel.y
from instrument,instrumentModel
where instrument.x=instrumentModel.y
You can use a join to select from 2 connected tables
select *
from Instrument i
join InstrumentModel m on m.InstrumentModelID = i.InstrumentModelID
When you use subqueries in the column list, they need to return exactly one value. You can read more in the documentation
as a user commented in the documentation, using subqueries like this can ruin your performance:
when the same subquery is used several times, mysql does not use this fact to optimize the query, so be careful not to run into performance problems.
example:
SELECT
col0,
(SELECT col1 FROM table1 WHERE table1.id = table0.id),
(SELECT col2 FROM table1 WHERE table1.id = table0.id)
FROM
table0
WHERE ...
the join of table0 with table1 is executed once for EACH subquery, leading to very bad performance for this kind of query.
Therefore you should rather join the tables, as described by the other answer.
How do I optimize the following update because the sub-query is being executed for each row in table a?
update
a
set
col = 1
where
col_foreign_id not in (select col_foreign_id in b)
You could potentially use an outer join where there are no matching records instead of your not in:
update table1 a
left join table2 b on a.col_foreign_id = b.col_foreign_id
set a.col = 1
where b.col_foreign_id is null
This should use a simple select type rather than a dependent subquery.
Your current query (or the one that actually works since the example in the OP doesn't look like it would) is potentially dangerous in that a NULL in b.col_foreign_id would cause nothing to match, and you'd update no rows.
not exists would also be something to look at if you want to replace not in.
I can't tell you that this will make your query any faster, but there is some good info here. You'll have to test in your environment.
Here's a SQL Fiddle illuminating the differences between in, exists, and outer join (check the rows returned, null handling, and execution plans).