简体   繁体   中英

MYSQL-Insert value in first empty row of column and read other values in same row

I have an sql statement that will insert a value to the first empty cell (first empty row in column). And if I ran the php script again, then it inserts into the next null cell etc. Problem: I also want to find out the ID of that row, and value of another column in that row. In the Mysql table below, I want a value inserted in the first null of COLUMN A , and also know the id and value in COLUMN B corresponding to that row (ie id=3 and COLUMN B= 11 ).

My_TABLE   
ID----  COLUMN A ---- COLUMN B  

1-------    6 -----------------        78  
2 ------   7  -----------------       90  
3 ------   NULL-------------      11  
4 ------   NULL-------------      5  
5  ------  NULL ------------     123

The following sql statement in PHP script will make it possible to insert value to the first empty cell in COLUMN A :

UPDATE My_TABLE  
SET COLUMN A = 83  
WHERE COLUMN A IS NULL  
LIMIT 1;

Result will be:

My_TABLE   
ID----  COLUMN A ---- COLUMN B  

1-------    6 -----------------        78  
2 ------   7  -----------------       90  
3 ------   83----------------      11  
4 ------   NULL-------------      5  
5  ------  NULL ------------     123

I also want to have an sql script that will print within PHP (echo) the values of ID and COLUMN B values corresponding to the first COLUMN A null value (ie ID= 3; COLUMN B= 11 ). How do I do that? Thank you.

Just print the result of the following query before the update query:

SELECT ID, COLUMN B FROM My_TABLE
WHERE COLUMN A IS NULL
LIMIT 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM