PHP MySQL insert data from one table to another table - php

I want to fetch data from one table to another, i have took references from many sites and Stackoverflow but I wasn't able to solve error.
The last field is the applicant field where I would like to send Default value 'No'.
I want to do this whole thing in single query
insert into user_identity (login_no, customer_id, prename,
fullname, mobile, dob, age, applicant) values
select login_id, customer_id, c_prename, CONCAT_WS(' ',`c_firstname`,`c_lastname`),
c_mobile, dob, age, 'No' from customer where id = '1'

the keyword VALUES is not needed,try this..
insert into user_identity (login_no, customer_id, prename,
fullname, mobile, dob, age, applicant)
select login_id, customer_id, c_prename, CONCAT_WS(' ',c_firstname,c_lastname),
c_mobile, dob, age, 'No' from customer where id = '1'
refer this.. http://dev.mysql.com/doc/refman/5.1/en/insert-select.html

When we are inserting from another table, we do not use the keyword values as we are not providing the values implicitly. we are fetching it from another table.
So in that sense, to create a table as an exact replica of another we can do something like this
CREATE TABLE EMP as SELECT * FROM EMPLOYEE
Similarly while inserting we do something like this
Insert into <tablename>(column name) select (column name) from <Table name>

Related

Moving columns between two tables in MySQL

I am working on PHP Codeigniter with MySQL database, I have two tables Student (Original table) and Student1 (temporary table) and both tables have different columns.
I am uploading bulk of student list from a CSV file into that Student1 (temporary table), later I have to move those student details into Student (Original table). How can I do this?
This is different case, I am not only asking about executing the query, instead I have to do it using MVC (CodeIgniter).
With this query:
INSERT INTO Student(first, last, dob) SELECT first, last, dob FROM Student1
Change column names to the appropriate column names of each table.
use this sql which help you
INSERT INTO Student(field1,field2,field3)
SELECT field1,field2,field2
FROM Student1;
you can use this code for ci
$query = $this->db->query('INSERT Student (field1, field2, field3)
SELECT field1, field2, field3
FROM Student');
this is format
INSERT INTO database2.table1 (field1,field2,field3)
SELECT table2.field1,table2.field2,table2.field3
FROM table2;
for more information
https://dev.mysql.com/doc/refman/5.7/en/insert-select.html
For example
Student has id and name;
Student1 has id, firstname, lastname
You could fill Student by request:
INSERT INTO Student SELECT id, CONCAT(firstname, ' ', lastname) FROM Student1;
If Student is not empty, you could append records (change id to null,
I assume that id column is autoincrement):
INSERT INTO Student SELECT null, CONCAT(firstname, ' ', lastname) FROM Student1;

How to combine 3 MySQL table inserts

So, I'm creating a user system in php. And of course users need to be inserted in my MySQL table. Now, I've got three tables that need to be filled:
- emails
---- id, email, subscriber
- addresses
---- id, street, street_number, zip_code, city, country, phone, btw_number, company
- users
---- id, username, password, salt, surname, name, joined, email_id, permissions_id, address_id, address_2_id
I could insert the email query, then select it's ID (auto inc) as id1. After that INSERT address and SELECT the id (auto inc) and then combine those 2 to INSERT in the users table. But is there a more quicker way so that I don't have to select the id each time after I've inserted the data?
There is a mysql function for that.
You can do the First user-insert and After that you can use LAST_INSERT_ID().
that Seems to be exactly what you need.
For More informations you can go here
And here you got some code examples

PHP MySql Insert columns form one table to another

I want to insert my table data from copying another table with some new given data.
I use this query
$sql = "INSERT INTO table2(name, city, email,money)
hasib,SELECT table1.city, table1.email,
newsletter_subscribers.email
FROM table1 WHERE name='jesy',100";
But its not Work
You must give all columns inside the select statement columns, even constant values, e.g.
insert into table2(name, city, email, money)
select 'hasib', city, email, 100
from table1
where name = 'jesy'
If you want to take values from multiple tables, as your select statement suggests, you must look into joins.

MYSQL / PHP - merge two tables with different columns

I want to access a database merging two tables from a php script in order to output the result to an html table. Both tables are pretty much the same except one column I need only exists in one table. I don't have direct access to the database.
INSERT INTO globalusers
(surname, firstname, email, location)
INSERT INTO localusers
(surname, firstname, email)
Location exists only in globalusers. I need to use this column as a dropdown filter in my table and add the column location with the string "local" to the data coming from the localusers table.
Here's a fiddle
So the expected result would be one table with four columns and the string "local" in the location column for the localusers table.
What is the select that I need?
MySQL version: 5.5.36
Sounds like you're looking to combine the results of the two tables. If so, you can use UNION ALL for this:
SELECT surname, firstname, email, location
FROM globalusers
UNION ALL
SELECT surname, firstname, email, 'local'
FROM localusers
Updated SQL Fiddle

mysql insert with a select

I am writing in PHP a mySQL query. I would like to duplicate a row in a table in the database. I would also like to insert a time stamp as to when that duplication happened with the NOW() function.
If I had a table named 'people' stuctured with the fields:
id, name, phone, dateCreated
and I wanted to duplicate this record I would do the following:
INSERT INTO people (id, name, phone, dateCreated)
SELECT id, name, phone, dateCreated
FROM people
WHERE id = '$id'
This will duplicate the record identified by the $id. I would like to do this same concept but instead of copying the dateCreated, I would like the query to insert the current datetime via the NOW() function but I am not sure how to structure this type of query.
Something like:
INSERT INTO people (id, name, phone)
SELECT id, name, phone
FROM people
WHERE id = '$id'
(dateCreated) value (NOW())
How would I structure the query?
INSERT INTO people (id, name, phone, dateCreated)
SELECT id, name, phone, NOW()
FROM people
WHERE id = '$id'

Categories