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;
Related
Hi I am stuck with one my sql view
CREATE OR REPLACE VIEW view_dr_server_actions_online AS
select
view_dr_server_actions.dr_request_id as dr_request_id,
view_dr_server_actions.server_id as server_id,
view_dr_server_actions.user_action as user_action,
view_dr_server_actions.action as action,
view_dr_server_actions.status as status,
view_dr_server_actions.appliance_id as appliance_id,
view_dr_server_actions.is_local as is_local,
view_servers.host_name as host_name,
view_servers.id as id,
view_servers.site_id as site_id,
view_servers.product_key as product_key,
view_servers.username as username,
view_servers.password as password,
view_servers.server_state as server_state,
view_servers.reachable_state as reachable_state,
view_servers.dr_plan_id as dr_plan_id,
view_servers.replication_site_id as replication_site_id,
view_servers.replication_appliance_id as replication_appliance_id,
view_servers.local_rpo as local_rpo,
view_servers.local_rto as local_rto,
view_servers.remote_rpo as remote_rpo,
view_servers.remote_rto as remote_rto,
view_servers.actual_local_rpo as actual_local_rpo,
view_servers.actual_local_rto as actual_local_rto,
view_servers.actual_remote_rpo as actual_remote_rpo,
view_servers.actual_remote_rto as actual_remote_rto,
view_servers.recovery_sequence_no as recovery_sequence_no,
view_servers.server_data as server_data,
get_product_data_of_server(view_dr_server_actions.dr_request_id) as drplan_data
from view_dr_server_actions
left join view_servers on view_servers.id = view_dr_server_actions.server_id
left join view_appliances_online on view_appliances_online.appliance_id = view_dr_server_actions.appliance_id
where server_id is not null
;
Above view returns 0 rows but if i run its inner query it returns result .
I am not sure what is happening exactly is that a problem with MySQL please help
Trying to insert into other table with the following command but it is neither throwing an error nor inserting:
INSERT INTO orderlines(orderLineItemName,orderLineItemnotes,itemID,itemPriceNormal,categoryID
,regionID,orderLineItemQuantity,orderLineItemPriceUnit,orderLineItemDiscount,orderLineItemSubTotal,
orderLineItemTotalTax
,orderLineItemStatus,userID,orderLineItemDateCreated,orderLineItemDateUpdated,ordercode)
SELECT quoteLineItemName, quoteLineItemnotes, itemID, itemPriceNormal, categoryID, regionID,
quoteLineItemQuantity, quoteLineItemPriceUnit, quoteLineItemDiscount, quoteLineItemSubTotal,
quoteLineItemTotalTax, quoteLineItemStatus, userID, quoteLineItemDateCreated, quoteLineItemDateUpdated,
'Complete' as orderstatus
FROM quoteslines
WHERE quotelineitemid = 1
try this
INSERT INTO orderlines
SELECT quoteLineItemName, quoteLineItemnotes, itemID, itemPriceNormal, categoryID, regionID,
quoteLineItemQuantity, quoteLineItemPriceUnit, quoteLineItemDiscount, quoteLineItemSubTotal,
quoteLineItemTotalTax, quoteLineItemStatus, userID, quoteLineItemDateCreated, quoteLineItemDateUpdated,
'Complete' as orderstatus
FROM quoteslines
WHERE quotelineitemid = 1
Is it correct to use "'Complete' as orderstatus" for the last column value. I don't think so. If you want to enter value "Complete" in the last column, please try below.
INSERT INTO orderlines(orderLineItemName,orderLineItemnotes,itemID,itemPriceNormal,categoryID
,regionID,orderLineItemQuantity,orderLineItemPriceUnit,orderLineItemDiscount,orderLineItemSubTotal,
orderLineItemTotalTax
,orderLineItemStatus,userID,orderLineItemDateCreated,orderLineItemDateUpdated,ordercode)
SELECT quoteLineItemName, quoteLineItemnotes, itemID, itemPriceNormal, categoryID, regionID,
quoteLineItemQuantity, quoteLineItemPriceUnit, quoteLineItemDiscount, quoteLineItemSubTotal,
quoteLineItemTotalTax, quoteLineItemStatus, userID, quoteLineItemDateCreated, quoteLineItemDateUpdated,
'Complete'
FROM quoteslines
WHERE quotelineitemid = 1
I'm spinning in circles trying to figure out what is likely a very simple SQL structure. My task seems simple - within the same table I need to update 3 related records with data from one master record. The master coordinates are in the record with a class of 'T', and I want to insert that record's coordinates into the rx_latitude/longitude columns of the related records with class code 'R'
The table structure is: callsign, class, tx_latitude, tx_longitude, rx_latitude, rx_longitude. Sample data looks like this:
J877, T, 40.01, -75.01, 0, 0
J877, R, 39.51, -75.21, 0, 0
J877, R, 40.25, -75.41, 0, 0
J877, R, 39.77, -75.61, 0, 0
Within that same table, I want to populate all of the rx_latitude and rx_longitude fields where the class is 'R' with the tx_latitude and tx_longitude coordinates where the class is 'T' and the callsign matches.
I've tried several insert and update statements, but I can only seem to operate on the master record, not the related records. I would appreciate any guidance that you might offer.
You can use UPDATE...FROM statement:
UPDATE theTable
SET
tx_latitude = masterRecord.tx_latitude,
tx_longitude = masterRecord.tx_longitude
FROM
(SELECT tx_latitude,tx_longitude,callsign FROM theTable WHERE class='T') masterRecord
WHERE
class='R' AND callsign = masterRecord.callsign
Updated
Try :
update yourTable t1, yourTable t2 set
t1.tx_latitude = t2.tx_latitude,
t1.tx_longitude = t2.tx_longitude
where t1.class = 'R' and t2.class = 'T' and t1.callsign = t2.callsign
Example
You can use MySQL's update ... join syntax.
It would go something like this:
update yourtable toUpdate
left join yourtable masterRecordTable
on toUpdate.callsign = masterRecordTable.callsign and masterRecordTable.class = 'T'
set toUpdate.rx_latitude = masterRecordTable.tx_latitude,
toUpdate.rx_longitude = masterRecordTable.tx_longitude
where toUpdate.callsign = 'J877' and toUpdate.class = 'R'
See this fiddle for a working example
I am having the following issue:
When I execute the following statement, I get an error for it returning more than one row
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow'),
(SELECT musicgenreid
FROM musicgenre
WHERE musicgenreid = 4),
( totallikes = 328374 ) );
I am getting the error on the (select pesonID from person...) statement, and I am trying to use the 'any' keyword to just grab any row, but I cannot seem to get it to work. I have tried just about any permutation I can think of of the current query and 'any', but it does not work. Is there another solution I should be trying or am I just missing the mark for some reason?
It seems you're trying to do something like this:
INSERT INTO artist (personid, musicgenreid, totallikes)
VALUES (
(SELECT personid FROM person
WHERE firstname = 'Joe' AND middlename = '' AND lastname = 'blow'
ORDER BY RAND()
LIMIT 1
),
4,
328374
);
This will get you a random personid that matches the given criteria.
The musicgenreid in your query would be either null or 4. I am forcing it to 4 as it seems that you're manually adding them and you know they already exist.
The total likes field is also fixed but your syntax was incorrect.
try with this sql statement
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow' LIMIT 1 ),
4,
328374);
Hello i have tree structure in sql. Logics is standard: SomeID, ParentID, other fields.
I have select procedure, which selects data like this:
1.
1.1
1.1.1
and so on.
How to write the select procedure, to get the inverted result(first are selected the deepest branches, last - root branches), like this:
1.1.1.
1.1.
1.
2.2.2.2.2.
2.2.2.2.
2.2.2.
2.2.
2.
and so on.
Non inversive select looks like thi(I use SqlServer 2008)s:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Object_SelectDownByRoot]
#ObjectID int
AS
WITH tree (ObjectID, ParentID, ObjectName, ObjectCode, DistrictID, DistrictName,
CityName, RegionName, StreetName, StreetID, AddressID, ObjectTypeName,
RouteName, ObjectTypeID, RouteID, AvrgTempIn, Area, Volume,
ElectricPower, ObjectStatusName, ObjectStatusID, [ControlRoom?], DateBuild,
[Floor], EncloseName, EncloseID, MaintenanceEval, AdministratorID,
Administrator, ElectricityPerson, ElectricityPersonID,
HeatingPersonID, HeatingPerson, HouseNo, FlatNo, ZIP,
AddressStreet, RouteCode, RouteDescription,
AddressDescription, StreetID2, CityID, AddressCityName) AS
(
SELECT
ObjectID, ParentID, ObjectName, ObjectCode, DistrictID, DistrictName,
CityName, RegionName, StreetName, StreetID, AddressID, ObjectTypeName,
RouteName, ObjectTypeID, RouteID, AvrgTempIn, Area, Volume,
ElectricPower, ObjectStatusName, ObjectStatusID, [ControlRoom?], DateBuild,
[Floor], EncloseName, EncloseID, MaintenanceEval, AdministratorID,
Administrator, ElectricityPerson, ElectricityPersonID,
HeatingPersonID, HeatingPerson, HouseNo, FlatNo, ZIP,
AddressStreet, RouteCode, RouteDescription,
AddressDescription, StreetID2, CityID, AddressCityName
FROM dbo.[ObjectQ] ofs
WHERE( ObjectID = #ObjectID )
UNION ALL
SELECT ofs.ObjectID, ofs.ParentID, ofs.ObjectName, ofs.ObjectCode, ofs.DistrictID, ofs.DistrictName,
ofs.CityName, ofs.RegionName, ofs.StreetName, ofs.StreetID, ofs.AddressID, ofs.ObjectTypeName,
ofs.RouteName, ofs.ObjectTypeID, ofs.RouteID, ofs.AvrgTempIn, ofs.Area, ofs.Volume,
ofs.ElectricPower, ofs.ObjectStatusName, ofs.ObjectStatusID, ofs.[ControlRoom?], ofs.DateBuild,
ofs.[Floor], ofs.EncloseName, ofs.EncloseID, ofs.MaintenanceEval, ofs.AdministratorID,
ofs.Administrator, ofs.ElectricityPerson, ofs.ElectricityPersonID,
ofs.HeatingPersonID, ofs.HeatingPerson, ofs.HouseNo, ofs.FlatNo, ofs.ZIP,
ofs.AddressStreet, ofs.RouteCode, ofs.RouteDescription,
ofs.AddressDescription, ofs.StreetID2, ofs.CityID, ofs.AddressCityName
FROM dbo.[ObjectQ] ofs
JOIN tree ON tree.ObjectID = ofs.ParentID
)
SELECT
ObjectID, ParentID, ObjectName, ObjectCode, DistrictID, DistrictName,
CityName, RegionName, StreetName, StreetID, AddressID, ObjectTypeName,
RouteName, ObjectTypeID, RouteID, AvrgTempIn, Area, Volume,
ElectricPower, ObjectStatusName, ObjectStatusID, [ControlRoom?], DateBuild,
[Floor], EncloseName, EncloseID, MaintenanceEval, AdministratorID,
Administrator, ElectricityPerson, ElectricityPersonID,
HeatingPersonID, HeatingPerson, HouseNo, FlatNo, ZIP,
AddressStreet, RouteCode, RouteDescription,
AddressDescription, StreetID2, CityID, AddressCityName
FROM tree
If you can't do recursion then I can only think of one other solution. I am sure it is not optimal but. You could do what you are doing above and insert that data into a temp table with 2 extra columns. The one column would hold your parent ID as it seems you are still sorting in descending order on the highest level (since you have all of the 1's before all of the 2's) and the other could just hold a seeded Identity integer. Then you could just query the table and sort on the original parent ID (the very first number) in ascending order and then the seeded Identity integer in descending order. From what I gather that would work but it would be inefficient.