Hi i'm writing a query that uses an insert and an update the update has a subquery which selects one or multiple rows.
When I run the query I get the following error: "#1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery"
The strange thing is that I am using the latest version of mysql and this feature was supported since 5.1. My query is as follows:
INSERT INTO Orders
(OrderID, Orderdate, Leverdate, status)
VALUES ('', now(), '2014-21-05', 'In Behandeling');
UPDATE Dozen SET OrderID = LAST_INSERT_ID()
WHERE OrderID IN (SELECT DoosID FROM Dozen Limit 0,3);
How can I rewrite this query to something mysql will understand and execute?
Thanks in advance!
use INNER JOIN
UPDATE Dozen
INNER JOIN Orders ON DoosID = OrderID
SET Dozen.OrderID = LAST_INSERT_ID()
Or change IN into = since your subquery anyways returns 1 record.
UPDATE Dozen
SET OrderID = LAST_INSERT_ID()
WHERE OrderID = (SELECT DoosID FROM Dozen LIMIT 1)
Related
Hope some of you pros can help me out on this sql / php issue.
The short version:
I need to add members to a task-database. So I have memberlist, it loop through each member and runs below sql.
I need to run an SQL statement that is to exit after first update / execution where it hits the parameters. So I need some kind of return for each time the sql updates a field?
Pseudocode:
Update this column
condition 1
condition 2
after first execution exit
Current sql:
UPDATE calendar
SET spil1 = '$temp'
WHERE spil1 IS NOT NULL
AND
(dayname = 'Lørdag'
OR dayname = 'Søndag')
// now exit if the above is met and the sql update was executed.
So the problem is I cannot make it stop (tried limit, top etc)
How is this made with SQL? or is there a smart way to condition it in the PHP loop before executing the script?
if you are using any unique id put this code at the end of your query..
good luck.
AND unique_id IN ( SELECT unique_id FROM calendar order by unique_id ASC LIMIT 1 )
Do you have an id column in calendar? If yes use the following query (Not tested):
UPDATE calendar
SET spil1 = '$temp'
WHERE id =
(SELECT id FROM
(SELECT * FROM calendar
WHERE spil1 IS NOT NULL
AND
(dayname = 'Lørdag'
OR dayname = 'Søndag')
LIMIT 1)T)
What this query does, it brings the first record that applies to your condition, and then update that record the way you want it
I'm pretty sure you want to assign a different member to each NULL value in the calendar table. This is tricky. It requires enumerating the rows in each table for the join -- and assumes a unique id in the calendar table.
update calendar c join
(select c.*,
row_number() over (order by c2.pil1) as seqnum
from calendar c2
where c2.pil1 is not null and
c2.dayname in ('Lørdag', 'Søndag')
) c2
on c.calendar_id = c2.calendar_id join -- the unique id
(select ml.*,
row_number() over (order by ml.member_id) as seqnum
from memberlist ml
) ml
on ml.seqnum = c2.seqnum
set c.spil1 = ml.member_id;
I also suspect that you want the condition for the calendar table to be IS NULL rather than IS NOT NULL, but this is the logic you have in the question.
Want to submit auto-increment value of table_a into table_b at same time. I first inserting record into table_a and then fetching last primary id from table_a and inserting it into table_b.
It works well at slow speed like 20 records per sec, but a fast speed and multi user level it inserting duplicate id of table_a into table_b.
Is my approach is wrong ? Please suggest better way to do this.
code
query1 = "insert into `table_a` (`aid`,`name`) values(null,'val')";
query2 = "select `id` from `table_a` order by `id` desc limit 1";
$aid='retrieved_value';
query3 = "insert into table_b (`bid`,`aid`,`btype`) values (null,'$aid','val')";
Yes, your approach is wrong. It is very racy. You have no guarantee that query2 returns the id related to query1. A better approach is to use the last_insert_id function.
i have a bunch of tables in one database. i am using three for testing and have created the following sqls
SELECT DISTINCT bb_users.user_nickname FROM bb_users
JOIN bucket_list ON bb_users.username = bucket_list.author
WHERE bucket_list.status=(:s) ORDER BY bucket_list.author ASC LIMIT 0, 30
This returns the user_nickname from the bb_users table and works as expected
I then use the following to do so on a table named music
SELECT DISTINCT bb_users.user_nickname FROM bb_users
JOIN music ON bb_users.username = music.author
WHERE music.status=(:s) ORDER BY music.author ASC LIMIT 0, 30
i run the sql query with the prepared statement:
//$sql ->the sql statements from above
$prep=$conn->prepare($sql);
$exec=$prep->execute(array(":s"=>"active")); var_dump($exec);
while($fetch=$prep->fetch(PDO::FETCH_ASSOC)){
//do stuff
}
the music tables outputs nothing and using php PDO the execute method returns false
both of these lines of code are dynamically created so i am very confused as to how one works and the other does not
any help is greatly appreciated
Changed the collation code from utf8_unicode_ci to utf8_general_ci
I want to perform a mysql UPDATE query and then get an array of ids that were effected in the change.
This is my update query
mysql_query("UPDATE table SET deleted='1' WHERE id='$id' OR foo='$foo' OR bar='$bar'");
I know that I can do something like this to get the created id from an INSERT query
mysql_query("INSERT INTO table (id,foo,bar) VALUES ('$id','$foo','$bar')");
$newid = mysql_insert_id();
I don't think MySQL has anything like the OUTPUT or RETURNING clauses that other databases support. You can get the list of ids by running a select before the update:
create table temp_table ids_to_update as
SELECT id
FROM table
WHERE (deleted <> '1' or deleted is null) and *id='$id' OR foo='$foo' OR bar='$bar');
Note that MySQL doesn't do an update when the value doesn't change. Hence the first condition -- which you may or may not find important.
Then, to ensure integrity (in the event of intervening transactions that change the data), you can do:
update table t join
temp_table tt
on t.id = tt.id
set deleted = '1';
You could also wrap the two queries in a single transaction, but I think using a temp table to store the ids is probably easier.
I am trying to write a dual MySQL query which archives the content of a table to another table, then deletes the original row in the original table.
I have the base query working just fine in other areas. The query below relies on a second table to help select which rows need to be archived & deleted.
I am getting a syntax error where the AS appears in the nested select of the first query and near the AS in the delete query.
I have researched and researched and tried a bunch of different code combos, but I can't get the queries working. The queries are written in PHP using PDO, so please ignore the PDO tags, they are not the problem.
INSERT INTO usetwca (r_id, c_id, o_id, t_id, s_id, ip_address, timestamp, timestamp_archived) SELECT :r_id, usetwc.c_id, usetwc.o_id, usetwc.t_id, usetwc.s_id, usetwc.ip_address, usetwc.timestamp, :timestamp FROM usetwc JOIN useo ON useo.o_id = usetwc.o_id AND useo.best_fit IS NULL WHERE usetwc.user_id = :user_id;
DELETE FROM usetwc JOIN useo ON useo.o_id = usetwc.o_id AND useo.best_fit IS NULL WHERE usetwc.user_id = :user_id;
You should implement this as a BEFORE DELETE trigger on the `users_sessions_exercise_t_ws_correlation' table.
Have a look at this answer as a good example.
Here's a good general reference on using Triggers for Logging.
I was able to get my queries working.
INSERT INTO usetwca (r_id, c_id, o_id, t_id, s_id, ip_address, timestamp, timestamp_archived) SELECT :r_id, usetwc.c_id, usetwc.o_id, usetwc.t_id, usetwc.s_id, usetwc.ip_address, usetwc.timestamp, :timestamp FROM usetwc JOIN useo ON useo.o_id = usetwc.o_id AND useo.best_fit IS NULL WHERE usetwc.user_id = :user_id;
DELETE usetwc FROM usetwc INNER JOIN useo ON useo.o_id = usetwc.o_id AND useo.best_fit IS NULL WHERE usetwc.user_id = :user_id