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
Related
We have two tables with same structure (MSSQL)
We want to compare
(ID) 122 from table1
with
highest ID 122 from table2
If there is one or more differences, we want to show the name of the column along with the data it holds from both tables.
I do not want to list all the columns out, as there is over 150
Something like (psuedo): -
Select * from table1
Select * from table2
Compare table1 (column by column) with table2 (column by column)
Show changes : -
'Name' WAS 'John' NOW 'James' CHANGED 3/11/2016 3.45pm
'Mobilenum" WAS '02373643743' NOW '0983783738' CHANGED 4/11/2016 12.46pm
Do not want to try triggers...need php code....
We should not need to give any column names as it should run though them all dynamically...
Why do you not want to list the columns - Is this table going to change regularly?
You can still create the SQL dynamically - but store it as a static string.
Lots of examples for this (hint use information_schema.columns)
If the tables are not huge then I would p[rob write a bunch of Tests (1-per column) and UNION All them together - Then only return the Data where diff = true
Eg
Select * from
(
Select case when ISNULL(table1.columnA, -999) <> ISNULL(table2.columnA, -999) then 1 else 0 end as diff, table1.columna as Old_Val, table2.columnA as new_val
from table1 join table2 on ...
UNION ALL
/* etc etc * 150 ... */
) as x
where x.diff = 1
Here is the situation i have two tables
of
idof, (somtother data)
---------------------
1, (somtother data)
2, (somtother data)
3, (somtother data)
and i have
ofc
id, ofid, idcat
------------------
1,1,1
1,1,2
1,2,1
1,3,3
Here is what i use to get all from first table and joint it with second
SELECT * FROM `of`
LEFT JOIN (`ofc`)
ON ( `of`.`idof` = `ofc`.`ofid` )
Now i want to get 5 rows from of (imagine that second table has more rows) for each idcat, and i cant seem to get the sql right. i have done till now
select * from `of` as `f`
JOIN `ofc` as `fa`
ON `f`.`idof`=`fa`.`ofid`
where (
select count(*) from `of` as `nf`
JOIN `ofc` as `nfa`
ON `nf`.`idof`=`nfa`.`ofid`
GROUP BY `nfa`.`idcat`
) <= 5;
Bit this subquery returns more than one row, and mysql complains. is there any other way to do this?without executing one query for each different idc?
I'm very new with SQL and need assistance on how I can accomplish this task using the correct query.
I have 2 tables that I need to use. Table "TB1" has:
id Name
1 bob
2 blow
3 joe
table "TB2" has:
compid property
1 bob
2 blow
I am trying to get which compid is missing in "TB2" and insert it from "TB1"
the query I am doing is:
SELECT id, name from TB1, TB2 where id <> compid
what I get is 2 ouputs of Id 1, and 2, and 3 outputs from id 3. by using php:
for($i=0;$i <= mysql_num_rows($comp)-1; $i++)
{
echo mysql_result($comp, $i, 0)."<br>";
}
and I expected the ouput 3 but instead got this:
1
1
2
2
3
3
3
I understand its comparing all the rows within the table but is there a way to achieve what I am looking for?
Thanks for your time.
You are performing an implicit Cartesian JOIN which results in every row against every other row. You need to specify what attribute JOINs the two tables.
Using implicit syntax (not recommended):
SELECT id, name
FROM TB1, TB2
WHERE id <> compid
AND TB1.Name = TB2.property <-- Column join
Using explicit syntax:
SELECT id, name
FROM TB1
JOIN TB2
ON TB2.property = TB1.Name <-- Column join
WHERE id <> compid
To accomplish your goal you would need something along the lines of:
SELECT TB1.id, TB1.name
FROM TB1
LEFT JOIN TB2
ON TB2.property = TB1.Name
WHERE TB2.compid IS NULL
See it in action
It's best practice to always alias the columns you select to prevent ambiguity.
To select it you can do:
SELECT *
FROM TB1
WHERE id NOT IN (
SELECT compid
FROM TB2
);
I have a reviews table that contains three ways to rate an item. The items themselves then have three columns to hold the average for each value respectively.
I could do this using three nested queries in an update query, but I feel like this is inefficient... Is there a way to update them all at once?
So far I've used this as my select query:
SELECT AVG(rating_1),AVG(rating_2),AVG(rating_3) FROM items_reviews WHERE item_id = 1
I just don't know how to use the result of that query to update an item row.
You could use an join in the UPDATE:
UPDATE items a
INNER JOIN
(
SELECT
item_id,
AVG(rating_1) AS avg1,
AVG(rating_2) AS avg2,
AVG(rating_3) AS avg3
FROM items_reviews
WHERE item_id = 1
GROUP BY item_id
) b ON a.item_id = b.item_id
SET
a.avgrating1 = b.avg1,
a.avgrating2 = b.avg2,
a.avgrating3 = b.avg3
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