Moving columns between two tables in MySQL - php

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;

Related

How can i insert data in table with a join sql?

i have 2 tables on db.
First table:
users
id|name|mail|password|group
second table
scores
id|name|score
The idea is get the name from users using the id (this id already know because i get that by php), then insert a score in table scores using the name obtained by a id.
I suppose that can i do with a inner join between users and scores.
How can i do that?
Insert into scores(id, name, score)
select ID, name, score(that you can pass)
from users
where id = (parameter pass by PHP)
Agree. You can simply use this:
INSERT INTO scores values (null, (select name from users where users.id= 1),100);
Replace the score and id with the values you get.
Assume you have the auto increment for your ids.

PHP MySQL insert data from one table to another table

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>

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.

how to insert data in 2 mysql table at a time with a foreign key

I have 2 mysql tables tbl_account and tbl_user . I want to add data into both tables using one form. I want to insert the following,
tbl_account = account_id, account_name, secret_key
tbl_user = user_id, account_id, user_name
What i'm doing is, inserting data in tbl_account then fetching account_id from that table and then inserting that same account_id in tbl_user. I'm fetching account_id through a secret_key which I pass in the form. Is this the right way or there is any simple way to do this. Please advice
In my opinion it would be easier to just put it in the 2 tables rather than inserting it in the first, reading it out, and then inserting again.
So i would do something like this:
$con = mysqli_connect("host","username","password","database");
mysqli_query($con,"INSERT INTO `tbl_account` (account_id, account_name) VALUES ('$account_id', '$account_name';");
mysqli_query($con,"INSERT INTO `tbl_user` (user_id , account_id, user_name) VALUES ('user_id', '$account_id', '$account_name';");

Insert the data from the data select in other table in PDO and MYSQL

There are two table
user:
id
uid
board:
id
message
user_id
The problem is , I got the uid now , and I would like to insert data to board, but I want to simplify all things to one query, so I tried
"INSERT INTO board (message,user_id) VALUES (:message, SELECT id FROM users WHERE uid=:uid)";
but in this way is not working, would anyone kindly teach me the right syntax? Thanks
Enclose SELECT in parentheses like this
"INSERT INTO board (message,user_id) VALUES (:message, (SELECT id FROM users WHERE uid=:uid))";
You either use a VALUES clause or a SELECT clause, not both.
INSERT INTO board (message, user_id)
SELECT :message, id
FROM users
WHERE uid = :uid

Categories