php mysql update and redistribute all rows - php

I wanted to update MySQL table with certain rules. I have let's say 3 column: ID, Name, Checked and I want to update Name to all rows equally where Checked = 0. In another table I have list 'Names` from where I pull my names.
Is this possible to do with one UPDATE query, or do i need to use some loop? Will there be a problem if let's say I have 11 rows to update but i have 10 names, how the query update those fields.

I find solution:
UPDATE `table` SET `ID` = (select `ID` from `name` ORDER BY rand() LIMIT 1) WHERE `Lock_ID` = 0
This query will randomly use name from other table and update it (it is 99% correct solution) it will redistribute name's almost the same for every row.

Related

MySql: How to Delete rows from a table EXCEPT a row with specific ID [duplicate]

This question already has answers here:
SQL WHERE condition is not equal to?
(10 answers)
Closed 2 years ago.
I have an html table with checkboxes. Users are permitted to delete rows from that html table except one specific one, whose value is hard coded in the DB.
If a user accidently checks it regardless, I want my server code (php) or even better the MySqL to run a delete query for all the rows they checked EXCEPT for that 1 specific row. So,something like this( which is wrong):
DELETE FROM table_name
WHERE row_id = some_value
EXCEPT row_id = some_value;
Many thanks !
I think the logic you want is AND:
DELETE FROM table_name
WHERE row_id = some_value AND row_id <> some_value;
Of course, some_value should be different most of the time.
DELETE FROM table_name WHERE row_id NOT IN (do_not_delete_this_value );
do_not_delete_this_value will be the id of the row you don't want to delete, rest all records will get deleted.
In fact there are a couple of ways to do it, for deleting all row's except one specific ID, type the following command in SQL query:
In between parentheses, type the ID number that you want to keep.
DELETE FROM `table_name` WHERE `ID` NOT IN (1)
Also, to delete all records except some id's, use this command:
DELETE FROM `table_name` WHERE `ID` NOT IN (1,2,6,10)

MySQL update query in PHP and get multiple ids returned

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.

How can i order by last update row

I have one Sql Query to get all the informations from my table.
I created an list using an foreach.
And i want to order this list, by the last updated row.
Like this
$query - "SELECT * FROM table ORDER BY last_updated_row";
//call Query here
And when i updated a certain row, i want to put this row on the top of the list
I heard about time_stamp, can i use time_stamp for that?
how can i do that?
Thanks
Assuming your using MySQL your table needs to be like this
CREATE TABLE table (
last_updated_row TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
That will give the row a create time stamp and update it on each update statement which effects the row
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
You can use just about any date/datetime/timestamp column in a table to sort by if needed. The only catch is you need to actually have it in the table.
Any of the above will allow sorts by ascending/descending order, but need to be maintained when inserting/updating a row.
Assuming you have the following structure:
table - someTable
id someVale updateTime
1 54634 ......
2 65138 ......
3 94141 ......
4 84351 ......
It doesn't matter what type of column updateTime is - whether it is a date, a datetime, a timestamp, a simple order by updateTime will work.
But you need to make sure that each insert/update you make to that row updates the column so that the sort will be true.

Updating Specific Column of Last 20 Rows in MySQL table?

I've been stuck on this for a while. I'm using PHP, and MySQL. What I'm trying to do is create a query to insert a new value into a column called "counter" starting from the second row all the way for 20 columns. The table needs to be sorted by 'article_id' in order to ensure that all the proper rows are updated.
If you can help me to do it for the 2nd row, I can apply that code to the other ones. So I only need the query to update one column at a time.
Table Structure:
Any thoughts?
As far as I can understand it, you are trying to do something like this:
SET #id = SELECT `article_id` FROM `table` ORDER BY `article_id` ASC LIMIT 1, 1;
UPDATE `table` SET `counter` = 1 WHERE `article_id` > #id ORDER BY `article_id` LIMIT 20;
Where of course table is your table name.

UPDATE certain row in SELECTED items with Certain id

I need to Select a column with name song_number where id = 2 and then update the second row from the selected rows with 7 for example
what i think that the query i need is something like this but i can't get it work
UPDATE `song` SET `song_number`= 7 WHERE (SELECT `song_number` FROM `song` WHERE `id` = 2 LIMIT 1,1)
any help will be appreciated
edit: i think the problem is mainly in the database structure i made however i found a solution to what i need by making stored procedure http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
so that i can save the selected items in a procedure and then update it
You have to identify which row you want to update. Identification means using a UNIQUE key or the PRIMARY key of the table.
The limitation of MySQL on UPDATE can be lifted by moving the condition from the WHERE to a JOIN:
UPDATE
song AS s
JOIN
( SELECT PK --- the Primary Key of the tba;e
FROM song
WHERE id = 2
ORDER BY ---whatever
LIMIT 1 OFFSET 1
) AS u
ON u.PK = s.PK
SET s.song_number= 7
If the PRIMARY KEY is id, then the above is useless of course. You are doing something wrong.
I doubt it is possible with one query and yet I see no reason in doing it in one query.
Why can't you just select and then update?
I think should be like this:
UPDATE `song` SET `song_number`= 7 WHERE `song_number` = (SELECT `song_number` FROM `song` WHERE `id` = 2 LIMIT 1,1);

Categories