I'm updating a table like so...
Update table Set count = count + 1 Where id = xx Limit 1
How to get the value of count without querying the table again? Can it be done in one query?
Thank you!
No.
Update does not return a result set.
However you can get the count without having to query the table
UPDATE `table` SET count = #count:= count + 1 WHERE id = 'xx' LIMIT 1;
SELECT #count as LastUpdateCount;
Related
I´m trying to insert into a table but it gives me the same error, and I dont understand why, because as I know, I can insert into a table multiple rows as long as the name columns match.
INSERT INTO top_semanal (id_usu, deporte)
SELECT DISTINCT actividades.dueno, actividades.deporte
FROM actividades
INNER JOIN usuario
ON actividades.dueno=usuario.id
WHERE actividades.deporte='Escalada'
ORDER BY usuario.Exp DESC
LIMIT 3;
This is the query. And as aditional info, it is very strange because with the same data in my database, sometimes work and sometimes doesn´t. And I need this query to be a trigger so I need this working the 100% of the times.
The goal here is get the 3 of the users with the most experience in the app, filtered by the sports they practice, and insert them into a "weekly top". I have a table "activities" where the users can add their activities, a table users and a table top3. The activities and user is related 1:M
¿Any help, I´m using xampp with mysql?
Edit: there is a trigger when the table is inserted:
BEGIN
UPDATE insignias_usuarios
SET proceso_Top = proceso_Top + 1
WHERE id_usu = NEW.id_usu;
IF ((SELECT proceso_Top FROM insignias_usuarios WHERE id_usu=NEW.id_usu AND proceso_Top=1 OR
proceso_Top=3)IS NOT NULL) THEN
UPDATE insignias_usuarios
SET nivel_Top = nivel_Top + 1, nivel_Top_Next = nivel_Top_Next + 1
WHERE id_usu = NEW.id_usu;
ELSEIF((SELECT proceso_Top FROM insignias_usuarios WHERE id_usu=NEW.id_usu AND proceso_Top=10)IS NOT NULL) THEN
UPDATE insignias_usuarios
SET nivel_Top = nivel_Top + 1, nivel_Top_Next = 1
WHERE id_usu = NEW.id_usu;
END IF;
END
¿May the error be because the trigger is called at the same time instead of 1 by 1 for each insert?
I currently use the below query to increment the third column count on every insert.
$DB2->query("INSERT INTO relations (item_ID,tag_ID,count)
SELECT '$ID', '$tag_id', MAX(count) + 1
FROM relations
WHERE tag_ID = '$tag_id';");
The problem is when there is no rows in the table and i try to insert, the Max(count) + 1 is just null. I've tried defining the default value as zero but still null. The column should be 1 on first insert.
How do i change the query, so if first insert then count is 1. I don't want to do a select query before because this code is in a loop.
add an ifnull(...,1)
"INSERT INTO relations (item_ID,tag_ID,count)
SELECT '$ID', '$tag_id', ifnull(MAX(count) + 1,1)
FROM relations
WHERE tag_ID = ''$tag_id';");
I have a table with 3 columns, as follows:
Columns:
ID
Channel_Location
Used
I would like to retrieve a random entry from the table and update Used column to 1. However, when I run my code - shown below - it returns 0 rows and doesn't return any idea. I was wondering why is this case?
The code
UPDATE channels
SET Used = 1
WHERE ID IN (
SELECT ID
FROM (select ID
FROM channels
WHERE Used != 0
ORDER BY RAND()
LIMIT 1) x);
One way to address this issue, is to do as follows:
SET #uid := (SELECT ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);
UPDATE channels SET Used = 1 WHERE ID = #uid;
SELECT * FROM channels WHERE ID = #uid;
In MySQL, how can I use use union with two update statements, or run two update statements simultaneously, so I get the count of affected rows of both (together)?
I'm trying to run the two of them together, instead of having two separate ones. The two of them update two different rows, so they cannot be run together in a single statement. Any help?
Example:
update prodb set buyer = buyer+1 where userId = 1
union
update prodb set seller = seller+1 where userId = 2;
Update: Did it this way, but it still returns affected rows as one. I guess it's returning the number of rows from the second statement only.
$stmt = '
update prodb set buyer = buyer+1 where userId = 1;
update prodb set seller = seller+1 where userId = 2;
';
$update = $dbc->prepare($stmt);
$update->execute();
Don't use union for update. Use multi_query (in case you are running php).
$db_link->multi_query("update prodb set buyer = buyer+1 where userId = 1; update prodb set seller = seller+1 where userId = 2;");
Never forget to add semicolon at last sql statement.
I just used this query
SELECT * FROM questions
ORDER BY RAND()
LIMIT 20;
On mysql database I have a column called display (along with the columns of questions) where the values are equal to 1.So now as you can see in the query I have limited to 20.I want to change the value of the all the 20 to display=0.I know this is the query
update test3
set display=0
where id=11;
But this just sets the display of one row.How do I do it for the 20.
Thanks in advance.
you can do this
update test3
set display=0
where id in (select id from questions order by rand() limit 20);
Suppose you are using php.
$result = msyql_query("select id from questions order by rand() limit 20");
$ids = array();
while($row = msyql_fetch_assoc($result)){
$ids[] = $row['id'];
}
For your condition, first perform the first query and save the ids from the first query.
Suppose you are using php and you have saved the ids from the first query in $ids;
you can run the second query like this;
$idstring = implode(',',$ids);
$query = 'update test3 set display=0 where id in ('.$idstring.' )';
$result = mysql_query($query);
Use the WHERE clause to search a subquery
UPDATE test3
SET display = 0
WHERE id IN (
SELECT id FROM questions
ORDER BY RAND()
LIMIT 20)
If you want to perform something between the SELECT and the UPDATE try this:
CREATE TABLE #Temp (
division TINYINT
)
INSERT INTO #Temp
SELECT id FROM questions
ORDER BY RAND()
LIMIT 20
--SELECT * FROM #Temp
UPDATE test3
SET display = 0
WHERE id IN (
SELECT division FROM #Temp)