I am trying to combine 3 tables into 1 in my database.
My table names = table1, table2, table3
I'm inserting the data into table4
Each table has columns of code, team, p1, p2, p3
IT IS NOT GOING INTO THE DATABASE.
I want the merged insert in the database to look like the following:
code, team, name 1, name 2, name 3, name 4, name 5, name 6, name 7, name 8, name 9
HERE ARE MY TABLES with COLUMNS
table 1: name 1, name 2, name 3
table 2: name 4, name 5, name 6
table 3: name 7, name 8, name 9
This is what I have so far but it is not working. Any help?
INSERT INTO table4
VALUES (code,team,name1,name2,name3,name4,name5,name6,name7,name8,name9)
SELECT table1.code1,
table1.team1,
table1.name1,
table1.name2,
table1.name3,
table2.name4,
table2.name5,
table2.name6,
talbe3.name7,
table3.name8,
table3.name9
FROM table1, table2, table3
WHERE table1.team = table2.team
AND table3.team = table1.team
AND table3.team = table2.team
INSERT INTO table4 VALUES ($code, $team, $name1, $name2, $name3, $name4, $name5, $name6,
$name7, $name8, $name9);
I think you were getting errors because you had a trailing comma after name 9, and you also shouldn't have spaces in the names. Assuming you didnt actually want spaces, we can also optimize your query into proper ANSI syntax:
INSERT INTO table4 (code,team,name1,name2,name3,name4,name5,name6,name7,name8,name9)
SELECT TableOne.code
,TableOne.team
,TableOne.name1
,TableOne.name2
,TableOne.name3
,TableTwo.name4
,TableTwo.name5
,TableTwo.name6
,TableThree.name7
,TableThree.name8
,TableThree.name9
FROM table1 AS TableOne
INNER JOIN table2 AS TableTwo ON TableTwo.team = TableOne.team
INNER JOIN table3 AS TableThree ON TableThree.team = TableTwo.team
The aliases assume code comes from table1. I assume you were getting an ambiguous error for your SELECT of team, on top of the trailing comma after name9.
ok, so dude ... as i understood your question, you are going to merging table or something similar.
Now in you query you are just selecting the data from three table, but nit inserting it into the 4 table.
for that you have to store SELECTED result into variable or array and later on insert it into the 4 table one by one ..
Take a reference of Google; if you stacked some where ...
All the best (Y)
Related
I have 3 database and I want to fetch data from 2 db and insert into another 1 but I have some problems doing it.
Database1 'first' : Table 'items':
id
name
price
1
Crystal
30
2
Ruby
20
Database 2 'second' : Table 'Shop':
id
name
items_id
state
1
KK shop
1
NY
2
ABC Supermarket
2
AL
I need to fetch data from table Items and search then 'name' in table Shop by using the 'id' from table Items.
Then I want to insert the data into table Details.
This is how it show look like, from empty to this:
Database 3 'third' : Table 'Details':
id
items.id
items.name
shop.name
1
1
Crystal
KK shop
2
2
Ruby
ABC Supermarket
My questions :
1)Since I have 3 database how should I connect to all of them in the same time? Should I create 3 php file to connect each of them and then include 3 of them in the same file?
2)If I want to copy all columns from a table to another table can I do like this? Will this copy all columns from 'first.items' to 'third.details'?
SELECT * from first.items
INSERT INTO third.details;
3)My SQL query,
SELECT * FROM first.items,
SELECT name FROM second.shop
WHERE first.items.id = 1,
INSERT INTO third.details (id,items.id,items.name,shop.name)
VALUES (1,first.items.id,first.items.name,second.shop.name);
After executing the query my table Details is unable to get any data, what is the problem? Thanks.
When you need to insert into some table data selected from other tables you can use next approach:
first: build query that select data that should be inserted. In your case in can be next:
SELECT 1, items.id, items.name, shop.name
FROM first.items
JOIN second.shop ON shop.id = items.id
WHERE items.id = 1;
second: combine the query with INSERT statement like:
INSERT INTO third.details (id, items_id, items_name, shop_name)
SELECT 1, items.id, items.name, shop.name
FROM first.items
JOIN second.shop ON shop.id = items.id
WHERE items.id = 1;
rename column : Database 2 'second' : Table 'Shop':
items.id to item_id
Database 3 'third' : Table 'Details':Set id as auto increment
Use join to combine first two tables:
SELECT items.id,items.name,shop.name FROM shop as shop left join items as items on items.id = shop.item_id WHERE items.id = 1,
INSERT INTO third.details (items.id,items.name,shop.name)
VALUES (first.items.id,first.items.name,second.shop.name);
Your INSERT INTO SELECT syntax is misplaced
In order to insert the entire table to the other one, the two must share compatible columns. Both counts and types.
The tables seem a bit unorganised but if you'd like to go through with it, the correct query should be as followed
INSERT INTO `third`.`Details` (`items.id`, `items.name`, `shop.name`)
SELECT `second`.`Shop`.items_id, `first`.`items`.name, `second`.`Shop`.name
FROM `first`.`items`, `second`.`Shop`
WHERE `second`.`Shop`.id = 1 and `first`.`item`.id = 1;
Also changing the where clause to
`second`.`Shop`.id = `first`.`items`.id;
should allow you to insert all the entries to your target table at once
I have 2 tables let say table1 & table2
table1 contains uniqueId, name1, name2, value fields
table2 contains id, uniqueName, keywords fields
table2.keyworks have comma separate names.
So, what I am trying to do is below.
select * from table1
//1> replace table1.name1 with table2.uniqueName if table2.keywords has
//table1.name1
//2> replace table1.name2 with table2.uniqueName if table2.keywords has
//table1.name2
select *,(case when FIND_IN_SET(table.name1,table2.keywords)>0 then table1.name1
when FIND_IN_SET(table.name1,table2.keywords)>0 then table1.name2 end)from table1
Try this.
I have table units in my database. In schema I have fields id, unit_id, group_id, city_id.
For simple I have 3 units:
(1, 1, 1, 1)
(2, 1, 2, 1)
(3, 1, 3, 2)
How can I remove useless groups id, when city id is the same. I have next result:
(1, 1, 1, 1)
(2, 1, 1, 1)
(3, 1, 3, 2)
I know how do this in PHP, but I think 'maybe MySQL has inbuild functions which i don't know' ;)
Regards
if I understand your question correctly you want to all group_id have same value from the same city_id. Basically your first table in question is what you have and the second one is desired result. If that's the case your query could look like this:
UPDATE table1
INNER JOIN (SELECT * FROM table1 GROUP BY city_id) AS tx
ON table1.city_id = tx.city_id
SET table1.group_id = tx.group_id;
Here is the SQL Fiddle to see how it's work.
If you want to completely remove values and to hold only distinct city_id then you can do that with query like this:
DELETE table1 FROM table1
INNER JOIN (SELECT * FROM table1 GROUP BY city_id) AS tx
ON table1.city_id = tx.city_id
WHERE table1.group_id != tx.group_id;
Here is SQL Fiddle for that!
In this case your result table will be without row with id 2...
GL!
If I understand correctly, you want to delete rows where group_id and city_id are equal? If so, it's very simple:
DELETE FROM units WHERE group_id = city_id
Okay, my solution:
UPDATE `ingame_units` INNER JOIN `ingame_groups` g1 ON `ingame_units`.`group_id`=g1.`id` LEFT JOIN `ingame_groups` g2 ON `ingame_units`.`group_id`<>g2.`id` AND g1.`city_id`=g2.`city_id` AND g1.`id`>g2.`id` AND g1.`game_id`=g2.`game_id` SET `ingame_units`.`group_id`=IFNULL(g2.`id`,g1.`id`)
Thanks one man to minus my post and don't try to help me. Regards :)
I have two tables and i try to create a table for managing data.
the first table is "questions":
questions (question_id, key1, key2, key3, key4, user_id, creation_date, class, type permission)
and the other:
questions_keys (question_key_id, question_key_name, question_key_refers_to)
every time i push the submit button stored in the table question_keys four records and one in table of questions. heres an example:
question_keys:
1, mathematics, 0
2, history, 1
3, physics, 2
4, geography, 3
and question table:
(1, 1, 2, 3, 4, 8, 2012-12-19 20:41:48, 4, multiple_choice, 0)
the query i need: in the table show the question_key_name where key1, key2, key3, key4 in table questions is equal to question_key_id.
and show in one row something like this:
mathematics, history, physics, geography, multiple_choice, 2012-12-19 20:41:48 etc
i'm new and i need your help.. thanks a lot!
Try this:
SELECT qk1.question_key_name qk2.question_key_name, qk3.question_key_name,
qk4.question_key_name, q.type, q.creation_date
FROM questions q
INNER JOIN question_keys qk1 ON q.key1 = qk1.question_key_id
INNER JOIN question_keys qk2 ON q.key2 = qk2.question_key_id
INNER JOIN question_keys qk3 ON q.key3 = qk3.question_key_id
INNER JOIN question_keys qk4 ON q.key4 = qk4.question_key_id;
You'll have to join the tables four times with this design:
SELECT question_key_name
FROM question_key qk
JOIN questions q on q.key1 = qk.question_key_id
WHERE q.question_id = ?
and then repeat for the other 3. You can UNION them all together, which would return you 4 rows.
As for returning only one row, check out GROUP_CONCAT, but that will return you the question_key_name values as one column. You may be better off combining the 4 rows in your app code.
I have 2 tables with the same columns.
for example
table 1
id
name
height
weight
table 2
id
name
height
weight
The data is table 2 is complete. But in table 1, only some data exists, and the rest of the columns are empty. for example:
table 1 table 2
id 4 4
name (empty) salman
height 5'11" 5'9"
weight (empyy) 65kg
I want a single script, that will allow me to update the table 2 with values from table 1, but only where it exists. In places where the table 1 is empty, I want to retain the data that already exists in table 2.
I've tried various ways, but all required multiple queries and are long and hectic. I want to know if there is a simpler way to get this done? Preferably in a single query?
Thank you
You can try by joining the 2 tables and then using the CASE keyword to conditionally update the fields:
UPDATE table2 t2 INNER JOIN table1 t1 USING (id)
SET
t2.name = CASE WHEN (t1.name IS NULL) THEN t2.name ELSE t1.name END
t2.height= CASE WHEN (t1.height IS NULL) THEN t2.height ELSE t1.height END
t2.weight = CASE WHEN (t1.weight IS NULL) THEN t2.weight ELSE t1.weight END
http://dev.mysql.com/doc/refman/5.5/en/case-statement.html
http://dev.mysql.com/doc/refman/5.5/en/update.html