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
Related
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
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>
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.
I have two tables, emp (pin, name, designation) and job (pin, date, type, group). If type is group in job table, I want the user to select multiple names from emp table. How this can be done in PHP?
the sql ="select * from emp,job where emp.pin=job.pin and job.group='' ";
I don't know you need this...
Please give the correct thing you want
I am writing a converter to transfer data from old systems to new systems. I am using php+mysql.
I have one table that contains millions records with duplicate entries. I want to transfer that data in a new table and remove all entries. I am using following queries and pseudo code to perform this task
select *
from table1
insert into table2
ON DUPLICATE KEY UPDATE customer_information = concat('$firstName',',','$lastName')
It takes ages to process one table :(
I am pondering that is it possible to use group by and get all grouped record automatically?
Other than going through each record and checking duplicate etc.?
For example
select *
from table1
group by firstName, lastName
insert into table 2 only one record and add all users'
first last name into column ALL_NAMES with comma
EDIT
There are different records for each customers with different information. Each row is called duplicated if first and last name of user is same. In new table, we will just add one customer and their bought product in different columns (we have only 4 products).
I don't know what you are trying to do with customer_information, but if you just want to transfer the non-duplicated set of data from one table to another, this will work:
INSERT IGNORE INTO table2(field1, field2, ... fieldx)
SELECT DISTINCT field1, field2, ... fieldx
FROM table1;
DISTINCT will take care of rows that are exact duplicates. But if you have rows that are only partial duplicates (like the same last and first names but a different email) then IGNORE can help. If you put a unique index on table2(lastname,firstname) then IGNORE will make sure that only the first record with lastnameX, firstnameY from table1 is inserted. Of course, you might not like which record of a pair of partial duplicates is chosen.
ETA
Now that you've updated your question, it appears that you want to put the values of multiple rows into one field. This is, generally speaking, a bad idea because when you denormalize your data this way you make it much less accessible. Also, if you are grouping by (lastname, firstname), there will not be names in allnames. Because of this, my example uses allemails instead. In any event, if you really need to do this, here's how:
INSERT INTO table2(lastname, firstname, allemails)
SELECT lastname, firstname, GROUP_CONCAT(email) as allemails
FROM table1
GROUP BY lastname, firstname;
If they are really duplicate rows (every field is the the same) then you can use:
select DISTINCT * from table1
instead of :
select * from table1