how to insert data in max id by using sql query - php

I want to get the maximum id of row data. In my table first column is id then firstname. this is the sql command I used to get the max(id) of row data and insert firstname in max(id) row. but it is not working
<?PHP $maxid=mysql_query("insert into test(firstname) values ('$sym') where id =max('id') ;?>

You can achieve your requirement in this way.
UPDATE test
SET firstname = '$sym'
ORDER BY `id` DESC LIMIT 1;

UPDATE yourtable
SET firstname = '$sym'
WHERE id in (SELECT max(id) from yourtable)

Related

How can i get the last record from database by php

Is there any way to get the last record from database without using:
SELECT * FROM 'myTabe' ORDER BY id DESC LIMIT 1
This code selects LAST INSERTED ROW:
SELECT * FROM 'mytable' WHERE id = LAST_INSERT_ID();

Finding the latest updated value only from table

Hey guys i have a table structure like
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
)
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1,'aff',2,3,5)
VALUES (2,'afff',1,31,52)
update CUSTOMERS
set NAME = 'somenewname'
where age = 1;
I just want to get output as 'somename'
I have tried creating a column updatedtime with timestamp and used query like
SELECT * FROM CUSTOMERS ORDER BY updatetime DESC LIMIT 1;
But this one shows the whole values ..
I just need only the updated value 'somenewname` as output.
I have also tried using triggers and functions but it didnt helped..
Please help me with a solution..I will accept it as an answer if it helps me ..remember i want an output as a single value like somenewname.
Thanx
The trigger which i have used
CREATE TRIGGER getrandom
AFTER UPDATE ON CUSTOMERS
FOR EACH ROW
BEGIN
UPDATE CUSTOMERS
SET NAME = 'anewname'
WHERE ADDRESS = 3;
END;
Why don't you try to SELECT only the name-column
SELECT name FROM CUSTOMERS ORDER BY updatetime DESC LIMIT 1; //outputs the name
instead of the whole result
SELECT * FROM CUSTOMERS ORDER BY updatetime DESC LIMIT 1;
Your UPDATE statement should look like
update CUSTOMERS
set NAME = 'somenewname',
updatetime = now()
where age = 1;
Now a example of how to get only the name if tables are joined.
SELECT c.name FROM CUSTOMERS as c INNER JOIN orders as o c.id=o.id ORDER BY c.updatetime DESC LIMIT 1;

Selecting the available previous row not working mysql php

Well, i found this simple script online somewhere for selecting the next row and selecting the previous row.
This works. (Next page)
$currentid = $_GET['id'];
$nextquery= mysqli_query($conDB,"SELECT * FROM vids WHERE ID > $currentid ORDER BY ID ASC LIMIT 1")or die (mysqli_error($conDB));
However, this dosent, it returns the current page id instead of the previous id.
What i want is that it gets the last available id that is on the database.
$prevquery= mysqli_query($conDB,"SELECT * FROM vids WHERE ID < $currentid ORDER BY ID desc LIMIT 1")or die (mysqli_error($conDB));
Please someone help me, i would greatly appreciate it! :D
Best regards Dániel
To find the largest value of the field "ID" from the "vids" table, you will want to use the following query:
SELECT ID FROM vids ORDER BY ID DESC LIMIT 1
To find the previous and next rows in the table, the queries you are using look correct, however there appears to be a typo in the first line calculating the $currentid:
$currrentid = $_GET=['id'];
This line should be:
$currrentid = $_GET['id'];
remove DESC for the previous query
"What i want is that it gets the last available id that is on the database."
`SELECT MAX(ID) FROM table_name;`
will give you the last record id in a table.
Next:
select * from table_name where ID = (select min(ID) from table_name where ID > $_GET['id'])
Previous:
select * from table_name where ID = (select max(ID) from table_name where ID < $_GET['id'])
Or you can optimise the query and have it all completed in one call:
select * from table_name where (
ID = IFNULL((select min(ID) from table_name where ID > $_GET['id']),0)
or ID = IFNULL((select max(ID) from table_name where ID < $_GET['id']),0)
)

mysql update subquery not working

mysql update syntax doesn't seem to work when I added the subquery.
Are there restrictions on subqueries on update syntax?
doesn't work:
update books set imagename ='name'
where book_ID='(select book_ID from books order by book_ID desc limit 1)';
works:
update books set imagename ='name'
where book_ID='101';
MySQL does have limits on subqueries using the table being updated. So yours is not allowed:
update books
set imagename ='name'
where book_ID = (select book_ID from books order by book_ID desc limit 1);
Instead, you can do:
update books
set imagename ='name'
order by book_ID desc
limit 1;
Here is the explanation in the documentation:
In general, you cannot modify a table and select from the same table
in a subquery. For example, this limitation applies to statements of
the following forms:
DELETE FROM t WHERE ... (SELECT ... FROM t ...);
UPDATE t ... WHERE col = (SELECT ... FROM t ...);
{INSERT|REPLACE} INTO t (SELECT ... FROM t ...);
Exception: The preceding prohibition does not apply if you are using a subquery for the modified table in the FROM clause. Example:
UPDATE t ... WHERE col = (SELECT * FROM (SELECT ... FROM t...) AS _t ...);
Here the result from the subquery in the FROM clause is stored as a temporary table, so the relevant rows in t have already been
selected by the time the update to t takes place.
Try This query.
Update books
Set imagename = name"
Where book_ID In (
Select T1.book_ID
From books As T1
Where T1.book_ID = '101'
)

How to get the last field in a Mysql database with PHP?

I have mysql database and I want to get the last ID from field in a table .
example : -
id Value
1 david
2 jone
3 chris
I want a function return 3 if this table exist .
You can use:
SELECT MAX(id) FROM table
If you want to select the ID of the most recently inserted row in a table with an AUTO_INCREMENT column, you will likey be interested in MySQL's LAST_INSERT_ID function.
SELECT id
FROM table
ORDER BY id DESC
LIMIT 1
you can also use the following code ::
$lastId = mysql_insert_id();
SELECT * FROM table ORDER BY id DESC LIMIT 1

Categories