Mysql insert query in after a chosen row? [duplicate] - php

This question already has answers here:
Is it possible to insert a new row at top of MySQL table?
(5 answers)
Closed 3 years ago.
I have persons in my database with their attended trainings but as time goes by, each of them will add some trainings again so i need to add it after that name.
I have table with columns id, firstname and training but that's my problem, inserting after a selected name
Sample
Insert into table (firstname, training) values ("Zarie", "Technical Writing Course") after a selected row where id = (select id where firstname="Zarie");
Thank you.

When you adding row to mysql table it goes to the end of table or on free primary key id. You can't change this when you inserting data. You can change order only when you selecting data:
SELECT * FROM table WHERE name = 'name' ORDER BY id DESC, firstname ASC
SELECT id, firstname FROM table WHERE name != 'Zimbo' ORDER BY firstname ASC

Related

Count field other than id field [duplicate]

This question already has answers here:
MySQL query to count non-null values in a single row
(2 answers)
Closed 4 years ago.
I have 5 columns in my SQL database
id, name, email, phone, and country
id: total row 54
email total row 45
How I will count how many emails in my database.
I use COUNT function, but not give actual results in case of email.
select count(company.email) from company
SELECT COUNT(company.email) FROM company WHERE company.email IS NOT NULL AND company.email <> ''
This query will exclude the rows that you have no email (either NULL or just empty field)
if email column contains null,
This should work.
SELECT COUNT(!ISNULL(email)) FROM company
select count(email) from company where company<>''
NULL will get off the count by themselves

i want to fetch all record from my database table without recent added record

i want to fetch all record from MySQL database table using PHP without last added record
I have 4 Columns in the Table, ID(auto) & title and message.
You can use this, if as you say, ID is auto_increment
$sql = "SELECT * FROM table WHERE ID != (SELECT MAX(ID) FROM TABLE) ORDER BY ID";
This is selecting all the records from your table, ordered by id, where the id is not the biggest one.

how to move data from one table to another after some days [duplicate]

This question already has answers here:
How to store day to day records from a mysql table to another?
(2 answers)
Closed 8 years ago.
is it possible to move one table column data to another column data after 1 or 2 days i mean like we have one table which is called table1 another table is table2 table1 have some data like name contact postal address then how to move all of data from table1 selected row to table2 after a period i mean one or two and when table1 data have move to table2 it will be delete from table1 .
in more details like we insert data into table1 like entry_day column using now() we get current date is there any option to after 1 days or 2 days the data will delete from tbl1 and insert into tbl2
$q= "INSERT INTO table1 (name,contact,postal,entrydate) values ('".$row[0]."','".$row[1]."','".$row[3]."',now())";
Just create a MySQL event to handle this based on the entrydate. Its quite easy and a quick google search will turn up plenty of information on how to create them.

Selecting the most recently added record to the database in Mysql [duplicate]

This question already has answers here:
PostgreSQL, SELECT from max id
(5 answers)
Closed 4 years ago.
I have a table in database which has a primary key called id. Now i want to display the most recently added 2 records into the table.
Assuming id is some sort of auto-incrementing integer value then the following will work
SELECT * FROM table ORDER BY id DESC LIMIT 2;
If you want just the last inserted record id (again assuming the insert generates an autoincrement id) there is also LAST_INSERT_ID. But beware this is global and will return last inserted id database-wide so it is not often used in SELECTS but rather as an OUT paramer in a routine to return the id of the just-inserted row.
SELECT LAST_INSERT_ID();
This wil definitely work,
SELECT * FROM table_name ORDER BY id DESC,LIMIT 2.
Try,
SELECT * FROM `table`
ORDER BY id
DESC
LIMIT 0, 2;

INSERT with SELECT in MySQL + PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the id of a row i've just inserted php/mysql
I was wondering what's the most efficient way of inserting something and selecting it's ID property at the same time?
For example, I have a table with auto incrementing primary ID column. I insert an item into that table and I need to know the autoincremented ID for use with another table.
Right now, what I do is:
INSERT INTO table1 the data
SELECT FROM table1 the ID
INSERT INTO table2 another set of data along with ID.
You can do this
INSERT INTO foo (auto,text)
VALUES(NULL,'text'); --generate ID by inserting NULL
INSERT INTO foo2 (id,text)
VALUES(LAST_INSERT_ID(),'text'); --use ID in second table
src: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
You can try mysql_insert_id();
http://php.net/manual/en/function.mysql-insert-id.php

Categories