Using Select & Insert in the same query - mysql

I have three tables and I'm trying to do a SELECT from one and INSERT to the other with using ON on the three columns with the same values.
Here's what I trying...
INSERT INTO
CustomerDetails_TEST
(Region_ID)
SELECT
RegionStatesUS.Region_ID
LEFT JOIN Customers ON Customers.Customer_ID = CustomerDetails_TEST.Customer_ID
LEFT JOIN RegionStatesUS ON RegionStatesUS.RegionState = Customers.BillingStateOrProv
FROM Customers
WHERE Customers.Customer_ID = CustomerDetails_TEST.Customer_ID
The tables are...
CustomerDetails_TEST
RegionStatesUS
Customers
And I'm trying to INSERT the value from RegionStatesUS.Region_ID using the Customer_ID INTO CustomerDetails_TEST.Region_ID
What am I missing here?

Your SQL syntax is wrong, I think you might want this:
INSERT INTO CustomerDetails_TEST (Region_ID)
SELECT r.Region_ID
FROM Customers c1
LEFT JOIN CustomerDetails_TEST c2
ON c1.Customer_ID = c2.Customer_ID
LEFT JOIN RegionStatesUS r
ON c1.BillingStateOrProv = r.RegionState
Update query would be similar to this not tested:
UPDATE CustomerDetails_TEST c1
LEFT JOIN Customers c2
ON c1.Customer_ID = c2.Customer_ID
LEFT JOIN RegionStatesUS r
ON c2.BillingStateOrProv = r.RegionState
SET c1.Region_ID = r.Region_ID

FROM comes before LEFT JOIN. The error message is hinting at that. Try again.

Couldn't see where to comment
just a note on how to update, or
insert into mytable
select from sometable
on duplicate key update
id = sometable.id;

Related

How to rename MySQL query result

How do I rename an output of a query?
I have this query:
select *
from (generalprofile left join applicant on applicant.profileID =
generalprofile.profileID) as A
inner join (generalprofile left join applicant on applicant.profileID =
generalprofile.profileID) as B on A.applicationID = B.applicationID ;
and I need to inner join the results of 2 queries.
"AS" doesnt seem to work
You don't build the select tables in right way
inside the ( ) you must place a valid select .. not only the table name and join
select *
from ( select * form generalprofile
left join applicant on applicant.profileID =
generalprofile.profileID) A
inner join (select * from generalprofile
left join applicant on applicant.profileID =
generalprofile.profileID) B on A.applicationID = B.applicationID ;

Using SUM function in an update query with inner joins

Getting an error when running this
UPDATE FlightBooking
INNER JOIN Passenger ON Passenger.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId = Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN ExtraCost ON ExtraCost.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN Luggage ON Luggage.LuggageId = ExtraCost.LuggageId
INNER JOIN SportsEquipment ON ExtraCost.SportsEquipmentId=SportsEquipment.SportsEquipmentId
INNER JOIN Insurance ON ExtraCost.InsuranceId = Insurance.InsuranceId
INNER JOIN CarHirePrice ON CarHirePrice.CarHirePriceId= ExtraCost.CarHirePriceId
INNER JOIN Route ON FlightBooking.RouteId = Route.RouteId
SET FlightBooking.TotalCost = (SUM(Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice))
WHERE FlightBooking.FlightBookingId=1;
When I have it formed as a Select query it returns the correct value so all the tables are fine. I'm assuming my syntax is wrong.
Any help would be appreciated.
You should use the following syntax when using an aggregate in an update statement.
UPDATE t1
SET t1.field = t2.field2Sum
FROM table1 t1
INNER JOIN (select field3, sum(field2) as field2Sum
from table2
group by field3) as t2
on t2.field3 = t1.field3
See Below, I only scripted a few tables but you can see how to do the rest.
UPDATE FlightBooking set TotalCost = (ExtraCost.SumExtra + SumLuggage) From FlightBooking
INNER JOIN Passenger ON Passenger.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId = Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN (Select FlightBookingId, sum(ExtraCost) as SumExtra from Extracost Group by FlightBookingId) as ExtraCost
ON ExtraCost.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN (Select FlightBookingId, sum(Price) as SumLuggage from Luggage Group by FlightBookingId) as Luggage
ON Luggage.FlightBookingId=FlightBooking.FlightBookingId
Are you sure that you have multiple records that match a single booking id?
If not, you can dispense with the sum():
SET FlightBooking.TotalCost = (Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)
If not, you'll need to pre-aggregate the tables that could generate multiple rows. In fact, you need to do this anyway to get a valid result (Cartesian products generated by joins will throw off the over sum).
many different possibilities for using aggregate funtion with update and inner join
UPDATE flight
SET flight.TotalCost = t.sumPrice
FROM FlightBooking AS flight
INNER JOIN
(
SELECT SUM(Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)) as sumPrice
FROM Passenger ON Passenger.FlightBookingId=flight.FlightBookingId
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId = Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN ExtraCost ON ExtraCost.FlightBookingId=flight.FlightBookingId
INNER JOIN Luggage ON Luggage.LuggageId = ExtraCost.LuggageId
INNER JOIN SportsEquipment ON ExtraCost.SportsEquipmentId=SportsEquipment.SportsEquipmentId
INNER JOIN Insurance ON ExtraCost.InsuranceId = Insurance.InsuranceId
INNER JOIN CarHirePrice ON CarHirePrice.CarHirePriceId= ExtraCost.CarHirePriceId
INNER JOIN Route ON flight.RouteId = Route.RouteId
) t
WHERE flight.bookingID = 1
also have a look to this examples
PC #PauloSantos and #OMGPonieshttps://stackoverflow.com/a/2009981/4426282

SQL not working when I choose some fields from the 1st table

I just used this code, and working properly with me:
SELECT
* FROM `order` as o
LEFT JOIN `services` as s ON s.`id` = o.`service_id`
LEFT JOIN `users` as u ON u.`id` = o.`users_id`
LEFT JOIN `files` as f ON f.`order_id` = o.`id`
but when I try to choose some fields from the 1st table, the results not showing the other tables
SELECT
o.`id` AS `id`,
o.`service_id`,
o.`extras`,
o.`quantity`,
o.`price`,
o.`links`,
o.`keywords`,
o.`status_id`,
o.`users_id`,
o.`date`,
o.`notes`,
o.`c_reason`,
o.`agent_star`
FROM `order` as o
LEFT JOIN `services` as s ON s.`id` = o.`service_id`
LEFT JOIN `users` as u ON u.`id` = o.`users_id`
LEFT JOIN `files` as f ON f.`order_id` = o.`id`
I don't know what is the exact error on the 2nd code, I need to show all columns from the tables: services, users & files
all columns or just defined columns
You when you select * from a join you are selecting all results from all tables involved.
When you are specifying orders you are only getting the results as they pertain to the orders table you get the same thing if you were to do SELECT o.* so if you want to see shared fields from different tables you have to specify them in your select statement as well.
Basically you're seeing the Different between SELECT * and SELECT o.*
This code is working for me, thanks everybody :)
SELECT
o.`id` AS `id`,
o.`service_id`,
o.`extras`,
o.`quantity`,
o.`price`,
o.`links`,
o.`keywords`,
o.`status_id`,
o.`users_id`,
o.`date`,
o.`notes`,
o.`c_reason`,
o.`agent_star`
s.*
u.*
f.*
FROM `order` as o
LEFT JOIN `services` as s ON s.`id` = o.`service_id`
LEFT JOIN `users` as u ON u.`id` = o.`users_id`
LEFT JOIN `files` as f ON f.`order_id` = o.`id`

mysql update query with inner join

I have two tables customer and order. I want to update different values in both tables with one query. For example customer table has a city column and value is germany and order table has status column and value is hold, I want to change germany to london and hold to resolved with one query. Here is the query below
UPDATE customer,order INNER JOIN order ON customer.cust_id = order.cust_id SET cust_city = 'Lahore' AND order_status= 'Resolved' WHERE cust_id = 2
mysql is showing error for this query
MySQL supports this operation:
UPDATE customer c INNER JOIN
order o
ON c.cust_id = o.cust_id
SET c.cust_city = 'Lahore',
o.order_status = 'Resolved'
WHERE c.cust_id = 2 ;
Note: order is a really bad name for a table, because it is a SQL keyword. Choose names for things that do not need to be escaped.
It is very simple to update using a join query in SQL.You can even join two or more tables. Here is an example :
UPDATE customer_table c
INNER JOIN
employee_table e
ON c.city_id = e.city_id
INNER JOIN
anyother_ table a
ON a.someID = e.someID
SET c.active = "Yes"
WHERE c.city = "New york";

Check id joining 3 tables

I have a MySQL JOIN query where 2 tables are joined to get the output
select distinct (a.error_type),a.links_id, a.crawl_cycle , b.* from $table a inner join crawler_error_type b on a.error_type = b.error_type where a.projects_id = '$pid' and b.error_priority_page_level='High'
Now I want to check if the value of the field error_type is present in the third table. If it is present then it shouldn't give me the resulted row.
Add the below:
LEFT OUTER JOIN third_table c ON c.error_type = a.error_type
and
WHERE c.error_type is null
LEFT OUTER JOIN will display all the records from a table joining on third_table. Since you do not want the record with matching error type from third_table, use WHERE c.error_type is null
You need to add one more INNER JOIN on third_table as:
SELECT DISTINCT a.error_type, a.links_id, a.crawl_cycle , b.*
FROM $table a
INNER JOIN crawler_error_type b
ON a.error_type = b.error_type
INNER JOIN third_table c
ON a.error_type = c.error_type
WHERE a.projects_id = '$pid' AND
b.error_priority_page_level='High'.
If I understand it correctly, you should just be able to inner join the third table. Inner joins will on show the record if they have records in the joining tables. If I am misunderstanding could you please elaborate. Thx
select distinct (a.error_type),a.links_id, a.crawl_cycle , b.*
from $table a
inner join crawler_error_type b on a.error_type = b.error_type
inner join some_table_3 c on c.error_type = a.error_type
where a.projects_id = '$pid' and b.error_priority_page_level='High'