I am using the below Query to try to insert a few rows from table2 in Database2 into table1 from Database1. Both these tables have different schemas.
Both the tables have same structure. I did check other posts with a similar query to no avail. Used the below basic approach :
INSERT INTO remoteDATABASE.remoteSCHEMA.remoteTABLE
SELECT * FROM localTABLE
INSERT INTO 'eno*****.com'.asdf_stage.temp
SELECT i.artf_id as id,
title as description,
(case
when i.assigned_group = 'asdfk' then 'dfg'
when i.assigned_group = 'dfgdg' then 'dgdk'
when i.assigned_group = 'dfghdgf' then 'igo'
when i.assigned_group = 'dfgh' then 'eMgem'
when i.assigned_group = 'Edsfg' then 'esgd'
end ) as area,
"N/A" as disposition,
"********" as flavor,
"ipf" as link,
"*************" as vendor,
DATE(date_created) date,
DATE(close_date) as completed
FROM ******com.lkjhg_stage.issues i
where date(date_created) >'2018-04-01' AND status!='Rejected'
I am getting syntax error, need to know what is the correct syntax to mention the DB name , Schema name and the table name at both the places.
Also, the table has around 10-15 columns , do I need to specify all of them ?
Replace the double quotes with single quotes. DATE is not a function, and the reserved word date is not a good column name.
Reference your table as SERVER.DATABASE.OWNER.OBJECT
INSERT INTO SERVER1.DATABASE1.OWNER1.OBJECT1
SELECT * FROM localTABLE
INSERT INTO SERVER1.DATABASE1.OWNER1.OBJECT1
SELECT i.artf_id as id,
title as description,
(case
when i.assigned_group = 'asdfk' then 'dfg'
when i.assigned_group = 'dfgdg' then 'dgdk'
when i.assigned_group = 'dfghdgf' then 'igo'
when i.assigned_group = 'dfgh' then 'eMgem'
when i.assigned_group = 'Edsfg' then 'esgd'
end ) as area,
'N/A' as disposition,
'********' as flavor,
'ipf' as link,
'*************' as vendor,
date_created,
close_date as completed
FROM SERVER2.DATABASE2.OWNER2.OBJECT2 i
WHERE date_created > '2018-04-01' AND status != Rejected'
Im trying to insert into a linked SQL table in access 2007, below is my query
INSERT INTO tblProducts ( ProductPrefix, ProductCode, ProductDescription, MadeFrom, MadeFromDescription, SamFamilySort1, SamFamilySort2, SamFamilySort3, SamFamilySort4, SamFamilySort5, Grade, Length, Thickness, fWidth, Factor, CubicMtrs, CubicMtrsFull, [Weight(T)], DrawingFilepath, EFACSProductGrouping, BatchSize, PackSize, Density, createdby, createddate, ProductType, customer, DimA, DimB, DimC, DimD, DimE, DimF, DimG, DimH, DimI, DimJ, DimK, DimL, DimM, DimN, DimO, DimP, DimQ, DimR, DimS, DimT, DimU, DimV, DimW, DimX, DimY, DimZ, TolA, TolB, TolC, TolD, TolE, TolF, TolG, TolH, TolI, TolJ, TolK, TolL, TolM, TolN, TolO, TolP, TolQ, TolR, TolS, TolT, TolU, TolV, TolW, TolX, TolY, TolZ, Dimension, Main, Saws, Moulders, PaintLines, XCut, DET, Wrapper, Blocks, HingeRecess, reorderpolicy, machinedaway, UseOtherM3XC, UseOtherM3MS, ShrinkWrap, ShrinkWrapPackSize, SW, samtype1, vtype1, vtype2, profile, productchamp, UOM, SAMPartGrp, PostingClass, ProductID )
SELECT DISTINCT tblProducts.ProductPrefix, tblProducts.ProductCode, tblProducts.ProductDescription, tblProducts.MadeFrom, tblProducts.MadeFromDescription, tblProducts.SamFamilySort1, tblProducts.SamFamilySort2, tblProducts.SamFamilySort3, tblProducts.SamFamilySort4, tblProducts.SamFamilySort5, tblProducts.Grade, tblProducts.Length, tblProducts.Thickness, tblProducts.fWidth, tblProducts.Factor, tblProducts.CubicMtrs, tblProducts.CubicMtrsFull, tblProducts.[Weight(T)], tblProducts.DrawingFilepath, tblProducts.EFACSProductGrouping, tblProducts.BatchSize, tblProducts.PackSize, tblProducts.Density, tblProducts.createdby, Date() AS Expr1, tblProducts.ProductType, tblProducts.customer, tblProducts.DimA, tblProducts.DimB, tblProducts.DimC, tblProducts.DimD, tblProducts.DimE, tblProducts.DimF, tblProducts.DimG, tblProducts.DimH, tblProducts.DimI, tblProducts.DimJ, tblProducts.DimK, tblProducts.DimL, tblProducts.DimM, tblProducts.DimN, tblProducts.DimO, tblProducts.DimP, tblProducts.DimQ, tblProducts.DimR, tblProducts.DimS, tblProducts.DimT, tblProducts.DimU, tblProducts.DimV, tblProducts.DimW, tblProducts.DimX, tblProducts.DimY, tblProducts.DimZ, tblProducts.TolA, tblProducts.TolB, tblProducts.TolC, tblProducts.TolD, tblProducts.TolE, tblProducts.TolF, tblProducts.TolG, tblProducts.TolH, tblProducts.TolI, tblProducts.TolJ, tblProducts.TolK, tblProducts.TolL, tblProducts.TolM, tblProducts.TolN, tblProducts.TolO, tblProducts.TolP, tblProducts.TolQ, tblProducts.TolR, tblProducts.TolS, tblProducts.TolT, tblProducts.TolU, tblProducts.TolV, tblProducts.TolW, tblProducts.TolX, tblProducts.TolY, tblProducts.TolZ, tblProducts.Dimension, tblProducts.Main, tblProducts.Saws, tblProducts.Moulders, tblProducts.PaintLines, tblProducts.XCut, tblProducts.DET, tblProducts.Wrapper, tblProducts.Blocks, tblProducts.HingeRecess, tblProducts.reorderpolicy, tblProducts.machinedaway, tblProducts.useotherm3XC, tblProducts.useotherm3MS, tblProducts.ShrinkWrap, tblProducts.ShrinkWrapPackSize, tblProducts.SW, tblProducts.samtype1, tblProducts.vtype1, tblProducts.vtype2, tblProducts.profile, tblProducts.productchamp, tblProducts.UOM, tblProducts.SAMPartGrp, tblProducts.PostingClass, tblProducts.ProductID
FROM tblProducts
This works fine and uploads all records in the table with new keys if i want to (I dont). I want to only recreate one product ive tried added the below
WHERE (((tblProducts.ProductID)=[tests]));
Where tests is a popupbox for User entry
i get an error below
My primary key in the table is called [ProductID]. It is possible to add a WHERE [ProductID] = 1234 in this query somehow?
Notice that the very last item in the column list of the INSERT INTO clause is ProductID. So, you are trying to insert a new row with an existing Primary Key value, and that won't work. As a simplified example,
INSERT INTO tblProducts (ProductDescription, ProductID)
SELECT tblProducts.ProductDescription, tblProducts.ProductID
FROM tblProducts
WHERE tblProducts.ProductID=1
will fail with a primary key violation. You need to remove ProductID from both the INSERT INTO and SELECT clauses, and only use it in the WHERE clause:
INSERT INTO tblProducts (ProductDescription)
SELECT tblProducts.ProductDescription
FROM tblProducts
WHERE tblProducts.ProductID=1
I am trying to clean up some MySQL code another programmer put together so that, whenever the state abbreviation is either QC (for Quebec) or ON (for Ontario), the sortOrder is set to 'ZZZZ.'
The MySQL query is pulling from a table with addresses of businesses, and the state field holds the abbreviation of the state or province, and the state_full field holds the full name of the state or province.
This is how the MySQL query looks at the moment:
SELECT DISTINCT
state_full,
state as sortOrder,
'' as state,
state as searchState
FROM vendorlocator
WHERE LENGTH(zip) < 6 OR ((LENGTH(zip) > 5) AND LOCATE('-',zip) > 0)
UNION
(SELECT DISTINCT
state_full,
'ZZZZ',
state,
state as searchState
FROM vendorlocator
WHERE LENGTH(zip) > 5 AND LOCATE('-', zip) = 0)
ORDER BY sortOrder, state
The way the other programmer set this up looks rather complex and messy, in my opinion..is there an easier way to set the value of sortOrder to 'ZZZZ' for addresses located in Quebec (QC) or Ontario (ON)?
You can use case expression in the select clause and >= instead of separate > and = in the where clause:
select distinct state_full
, state as sortOrder
, case when (length(zip) > 5 and locate('-',zip) = 0) then 'ZZZZ' else '' as state
, state as searchState
from vendorlocator
where length(zip) < 6 or ((length(zip) > 5) and locate('-',zip) >= 0)
I have a table, named city.
city_name
---------------
New York
Beijing
New York
New York
Dubai
Beijing
---------------
After update, I want it be:
city_name, index
---------------
New York, 0
Beijing, 0
New York, 1
New York, 2
Dubai, 0
Beijing, 1
---------------
The pattern is like this: the first New York will be have an index of 0, the second New York's index is 1 and the third one will be 2. There are millions of rows in this table.
Any easy way to make this update?
I am thinking to solve this problem in two step.
First step:
#cities = Select distinct city_name from city;
Second step:
foreach #cities as #city
update city set index = row_num where city_name = #city.cityname
It seems row_num is not availbe in mysql.
Try this:
update city cross join
(select #city := '', #prevcity := '', #i := 0) const
set `index` = (case when (#prevcity := #city) is null then null
when (#city := city) is null then null
else #i := if(#prevcity = city, #i + 1, 1) is null then null
end)
order by city;
If you are familiar with the use of variables for enumeration in a select statement, then this is similar. The complication is ensuring the order of evaluation for the update. This is handled by using a case statement, which sequentially evaluates each clause until one is true. The first two are guaranteed to be false (because the values should never be NULL).
EDIT:
If you have a unique id, then the solution is a bit easier. I wish you could do this:
update city c
set `index` = (select count(*) from city c2 where c2.city = c.city and c2.id <= c.id);
But instead, you can do it with more joins:
update city c join
(select id, (select count(*) from city c2 where c2.city = c1.city and c2.id <= c1.id) as newind
from city c1
) ci
on c.id = ci.id
set c.`index` = ci.newind;
A way to do this using session() for storage AND comparing purposes:
session_start();
$number=0;
$result=mysqli_query($yourconnection,"SELECT * FROM city ORDER BY city_name");
while($row=mysqli_fetch_array($result)){
if(empty($_SESSION["storage"])){
/* THIS CONDITION ONLY GOES THROUGH THE VERY FIRST ARRAY FETCH */
$_SESSION["storage"]=$row['city_name'];
mysqli_query($yourconnection, "UPDATE city SET index='$number' WHERE city_name='$cityname'"); /* STORE TO THE FIRST CITY'S INDEX 0 */
}
else if($_SESSION["storage"]==$row['city_name']){
/* IF SESSION IS THE SAME COUNTRY AS THE CURRENT ROW COUNTRY */
$_SESSION["storage"]=$row['city_name'];
$number=$number+1; /* INCREMENT NUMBER FOR THE SAME COUNTRY */
mysqli_query($yourconnection, "UPDATE city SET index='$number' WHERE city_name='".$row['city_name']."'");
}
else {
/* THIS IS FOR THE NEXT NEW COUNTRY */
$number=0; /* START AGAIN THE COUNT TO 0 IF NEW COUNTRY */
$_SESSION["storage"]=$row['city_name'];
mysqli_query($yourconnection, "UPDATE city SET index='$number' WHERE city_name='".$row['city_name']."'");
}
} /* END OF WHILE LOOP */
I've done this before, but with different output but with the same logic. AND I use another table for storage purposes and comparing purposes. But the code above that I've made, I used session instead.
This is the query I'm trying to accomplish:
update amdashboard
set (ASCID, ASCFirst, ASCLast, ASCOtherName, ASCAdd1, ASCAdd2,
ASCCity, ASCState, ASCZip, ASCZip4, ASCY2007, ASCY2008, ASCY2009,
ASCY2010, ASCY2011, ASCY2012, ASCEthnicity, ASCGender, ASCMaritalStatus)
= (select id, firstname, lastname, listingspousename, add1, add2,
city, state, zip, zip4, y2007, y2008, y2009,
y2010, y2011, y2012, Ethnicity, Gender, MaritialStatus
from ASCNCOAClean
inner join amdashboard
on ASCNCOAClean.firstname = amdashboard.actorsfirst
and ascncoaclean.lastname = amdashboard.actorslast)
where exists (select id, firstname, lastname, listingspousename,
add1, add2, city, state, zip, zip4, y2007, y2008,
y2009, y2010, y2011, y2012, Ethnicity, Gender,
MaritialStatus
from ASCNCOAClean
inner join amdashboard
on ASCNCOAClean.firstname = amdashboard.actorsfirst
and ascncoaclean.lastname = amdashboard.actorslast);
I can't get this to work...receiving a syntax error on the first parenthesis. So, I figured I'd try on just one field. I tried this:
update amdashboard
set ascid = (select ascncoaclean.id
from ASCNCOAClean
where ASCNCOAClean.firstname = amdashboard.actorsfirst
and ascncoaclean.lastname = amdashboard.actorslast)
where exists (select ascncoaclean.id
from ASCNCOAClean
where ASCNCOAClean.firstname = amdashboard.actorsfirst
and ascncoaclean.lastname = amdashboard.actorslast);
This however returns and error 1242: Subquery returns more than 1 row. That seems silly. I know it's going to return more than one row...I want it to because I need to update multiple rows.
What am I missing?
The query you want would look something like this:
UPDATE amdashboard a, ASCNCOAClean b SET
a.ASCID = b.id,
a.ASCFirst = b.firstname,
a.ASCLast = b.lastname,
a.ASCOtherName = b.listingspousename,
...
a.ASCMaritalStatus = b.MaritialStatus
WHERE a.actorsfirst = b.firstname;
Observe you will have to replace ... with the rest of the column associations I didn't write.
But be careful with that, something tells me this query is going to do something very wrong to your database, because you are not relating the tables using a unique key. If there are two records with the same ASCNCOAClean.firstname you certainly will have loss of data.
Also observe that it is going to update existing records on amdashboard, not add new ones. If your intention is to migrate data from ASCNCOAClean to amdashboard, assuming amdashboard is a brand new, empty table, then the query you want is this:
INSERT INTO amdashboard (
ASCID, ASCFirst, ASCLast, ASCOtherName, ASCAdd1, ASCAdd2, ASCCity, ASCState,
ASCZip, ASCZip4, ASCY2007, ASCY2008, ASCY2009, ASCY2010, ASCY2011, ASCY2012,
ASCEthnicity, ASCGender, ASCMaritalStatus
)
SELECT
id, firstname, lastname, listingspousename, add1, add2, city, state,
zip, zip4, y2007, y2008, y2009, y2010, y2011, y2012, Ethnicity, Gender,
MaritialStatus
FROM ASCNCOAClean;