I am having 3 tables named customers,cars,carrent. All i want is to multiply rentorder.days with cardetail.rentday and show the value in rentorder.totalrent. I am unable to achieve this. How can i do this.
Any suggestions please.
SQL
SELECT customers.*,
cardetail.carname,
cardetail.model,
cardetail.company,
cardetail.color,
cardetail.rentday,
rentorder.days,
rentorder.totalrent
FROM rentorder
INNER JOIN customers
ON customers.custid = rentorder.custid
INNER JOIN cardetail
ON cardetail.id = rentorder.carid
Just multiply rentday with days to get the computed value totalrent in Select list.
Try this
SELECT customers.*, -- Not sure this is allowed in [Mysql]
cardetail.carname,
cardetail.model,
cardetail.company,
cardetail.color,
cardetail.rentday,
rentorder.days,
cardetail.rentday * rentorder.days AS totalrent
FROM rentorder
INNER JOIN customers
ON customers.custid = rentorder.custid
INNER JOIN cardetail
ON cardetail.id = rentorder.carid
To save the data you need to use update from Inner Join syntax
update rentorder
INNER JOIN customers
ON customers.custid = rentorder.custid
INNER JOIN cardetail
ON cardetail.id = rentorder.carid
SET
rentorder.totalrent = cardetail.rentday * rentorder.days
Related
I am building a table in reporting services and am not getting the results I expect. It appears that my table is taking my data and multiplying by 3. In the example below the quantity should be 1 and the ext price should be roughly $322. I have spot checked a few of these orders in our system and it appears it is doing this across the board. Below is the query I built but I am not sure what is causing the issue. Any suggestions? Thanks!
SELECT
OrderDetail.Quantity
,OrderDetail.PartNo
,OrderDetail.UnitPrice
,Orders.OrderNumber
,Orders.ReqDate
,Orders.ShpAddr_City
,Orders.ShpAddr_State
,act.PartMapping.SalesRevenue
,MasterPartList.ProductCategoryID
,Categories.Description
,OrderDetail.LocationID
,Orders.InvAddrCompanyName
,Orders.Status
,CustomerUserDefValues.UserDefValue
,UserDefs.UserDefID
,Orders.ClosedDate
FROM
OrderDetail
INNER JOIN Orders
ON OrderDetail.oKey = Orders.oKey
INNER JOIN act.PartMapping
ON OrderDetail.PartNo = act.PartMapping.PartNo
INNER JOIN MasterPartList
ON OrderDetail.PartNo = MasterPartList.MasterPartNo
INNER JOIN Categories
ON MasterPartList.ProductCategoryID = Categories.ID
INNER JOIN CustomerUserDefValues
ON Orders.CustomerID = CustomerUserDefValues.CustomerID AND Orders.SiteID =
CustomerUserDefValues.SiteID
INNER JOIN UserDefs
ON CustomerUserDefValues.UserDefID = UserDefs.UserDefID
WHERE dbo.UserDefs.[UserDefID] in (#UserDefID) and act.PartMapping.[SalesRevenue] in
(#ValueAddAccount)
I have 8 tables in my database, I want to get their data from database using query,.
My first table has master_id and I have passed it in my every table after master table. I want to fetch all data with master_id that I have passed in my tables.
I got some syntax errors in it.
SELECT edu_tb.* FROM edu_tb
INNER JOIN master_tb ON
master_tb.`master_id` = edu_tb.`master_id`
AND
// table 2 experience
SELECT exp_tb.* FROM exp_tb
INNER JOIN master_tb ON
master_tb.`master_id` = exp_tb.`master_id`
AND
// table 3 certificate
SELECT certi_tb.* FROM exp_tb
INNER JOIN master_tb ON
master_tb.`master_id` = certi_tb.`master_id`
It is not recommended to use * to retrieve fields from a query, especially when multiple tables are involved. The query below probably will be problematic because of the asterisks.
SELECT edu_tb.*, exp_tb.*, certi_tb.*
FROM master_tb
INNER JOIN edu_tb ON edu_tb.`master_id` = master_tb.`master_id`
INNER JOIN exp_tb ON exp_tb.`master_id` = master_tb.`master_id'
INNER JOIN certi_tb ON certi_tb.`master_id` = master_tb.`master_id
Something along the line of:
SELECT * FROM master_tb mt
INNER JOIN edu_tb et ON mt.master_id = et.master_id
INNER JOIN exp_tb ex_t ON mt.master_id = ex_t.master_id
INNER JOIN certi_tb ct ON mt.master_id = ct.master_id;
To get a result in the phpmyadmin,
SELECT edu_tb.* FROM edu_tb
INNER JOIN master_tb ON
master_tb.`master_id` = edu_tb.`master_id`
--AND
-- table 2 experience
SELECT exp_tb.* FROM exp_tb
INNER JOIN master_tb ON
master_tb.`master_id` = exp_tb.`master_id`
--AND
--table 3 certificate
SELECT certi_tb.* FROM certi_tb
INNER JOIN master_tb ON
master_tb.`master_id` = certi_tb.`master_id`
This query results in 3 different table outputs on the myadmin query result are.
If you are trying to get a data-set into php code, the output of only one select statement will be received by php(the output of the last select statement).
So joining all this tables together would get you all this tables included in a single select statement.
SELECT * FROM edu_tb
INNER JOIN master_tb ON
master_tb.`master_id` = edu_tb.`master_id`
INNER JOIN exp_tb ON
master_tb.`master_id` = exp_tb.`master_id`
INNER JOIN certi_tb ON
master_tb.`master_id` = certi_tb.`master_id`
hope this helps.
I have 2 tables that need updating based on 2 where clauses
i included a 3rd table which would join the other 2 tables together. i cant get either working.
UPDATE (list INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID) INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET
Players.Players_Team_ID = 6, Users.users_bank = users_bank-15000000, list.transfers = list.transfers+1
WHERE Users.User_ID=14 AND Players.Players_ID=3;
Without the 3rd table having an update it would be
UPDATE (list INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID) INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET
Players.Players_Team_ID = 6, Users.users_bank = users_bank-15000000
WHERE Users.User_ID=14 AND Players.Players_ID=3;
Can anyone help me get this working?
You can change your query to be like below using update-join syntax but I don't see why you need to JOIN with other tables. Your UPDATE statements could be single or individual update as well
UPDATE list,Players,users
INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID
INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET Players.Players_Team_ID = 6,
Users.users_bank = users_bank - 15000000,
list.transfers = list.transfers + 1
WHERE Users.User_ID=14
AND Players.Players_ID=3;
In Access I have a table like this:
data(BillNo number,acno number)
agro(BillNo number,Price number,qty number)
account(acno,Name)
I want an output like:
account.acno,account.Name,sum(agro.Price*agro.qty)
My query is:
SELECT account.accountnumber,
account.name,
Sum(agro.price * agro.qty)
FROM account
INNER JOIN (agro
INNER JOIN data
ON agro.billno = data.billno)
ON account.accountnumber = data.acno;
But it does not work. Please help me.
You've mixed up the text in your query. Should be something like:
SELECT account.acno, account.Name,Sum(agro.Price*agro.qty)
FROM account
INNER JOIN data ON account.acno= data.acno;
INNER JOIN agro On data .BillNo = agro.BillNo
GROUP BY account.acno, account.name
So I have the following table, do_stock_movement, that looks like this:
stock_movement_id sm_number sm_source_id sm_destination_id
15164b86a7533d 145 1516478840ee29 151644d8bd63f2
15166b89d1a9fc 194 15165c481bd9d0 151659e632cd48
The columns sm_source_id and sm_destination_id both reference product points stored in do_product_points.
I'm using the following SQL query:
SELECT * FROM do_stock_movement
INNER JOIN do_product_points ON product_points_id = sm_source_id
WHERE sm_number = '145'
In do_product_points, there's a field called pp_name. I need the corresponding pp_name for both sm_source_id and sm_destination_id.
However, the query above will only return the pp_name for sm_source_id, or for sm_destination_id if you change the joined field to that.
What SQL query will return the corresponding pp_name for both sm_source_id and sm_destination_id?
I hope this is clear. Please ask questions if it isn't!
JOIN this table do_product_points one more times for the sm_destination_id:
SELECT
s.pp_name AS SourcePoint,
d.pp_name AS DestinationPoint,
...
FROM do_stock_movement AS m
INNER JOIN do_product_points s ON s.product_points_id = m.sm_source_id
INNER JOIN do_product_points d ON d.product_points_id = m.sm_destination_id
WHERE m.sm_number = '145'
You need join twice and use alias:
SELECT *, Src.pp_name, Dst.pp_name FROM do_stock_movement
INNER JOIN do_product_points as Src
ON Src.product_points_id = sm_source_id
INNER JOIN do_product_points as Dst
ON Dst.product_points_id = sm_destination_id
You need to join to the product_points table twice, once with source_id and once with destination_id:
SELECT * FROM do_stock_movement move
INNER JOIN do_product_points source ON source.product_points_id = move.sm_source_id
INNER JOIN do_product_points dest ON dest.product_points_id = move.sm_destination_id
WHERE sm_number = '145'