How to Perform Innerjoin in access - mysql

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

Related

How to get information from multiple tables in database with single id?

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.

Don't show any record given a condition (SQL)

Good morning, I am trying to perform the following query in MySQL:
Show the name and surname of the consultants who have not participated in any "Juan Perez" project.
I'm using the following query:
SELECT consultant.name, consultant.surname FROM consultor
INNER JOIN participate ON participate.id_consultant = consultant.id
INNER JOIN project ON project.id = participate.id_project
INNER JOIN cliente ON client.id = project.id_client
WHERE client.name NOT IN("Juan Perez")
But when I execute the query, it only hides those that are directly related in the tables.
How could I hide the other records where the consultants appear so that they do not appear?
Thanks.
I believe it should work if on the conditions of the join to client you exclude the client name there that it will return you what you want.
SELECT consultant.name, consultant.surname
FROM consultor
INNER JOIN participate ON participate.id_consultant = consultant.id
INNER JOIN project ON project.id = participate.id_project
INNER JOIN client ON client.id = project.id_client and client.name NOT IN ("Juan Perez");
I managed to solve it in the following way. Thanks to all for the help.
SELECT consultant.name, consultant.surname FROM consultant
LEFT JOIN (
SELECT id_project, participate.id_consultant FROM participate
INNER JOIN project
USING(id_project)
INNER JOIN client ON client.id = project.id_cliente
WHERE client.name like 'Juan Perez'
)
project ON project.id_consultant = consultant.id
WHERE project.id_project IS NULL;

Multiply two columns and display in new one

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

Get mysql SUM after mathematical operation

Here is a portion of my query
(SUM(dr.drv)/100)*
(st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value)
This will generate multiple rows. I want to get SUM of all of them. I tried
SUM(SUM(dr.drv)/100)*
(st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value)
as total drv_amount
But it's throwing an error. Can someone guide me to the right direction?
Here is the whole query:
SELECT SUM(dr.tickets_sold) as tickets_total_amount,
SUM(dr.drv) as drv_total_amount,
FORMAT(((SUM(dr.drv)/100) * (st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value) ),2 ) as supplier_commission,
FORMAT(((SUM(dr.drv)/100) * st.exhibitor_value),2) as exhibitor_commission,
FORMAT(((SUM(dr.drv)/100) * st.circuit_value),2) as distributer_commission,
FORMAT(((SUM(dr.drv)/100) * (st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value)+((SUM(dr.drv)/100) * st.circuit_value )),2) as film_hire_total
FROM com_pro_dr as dr
INNER JOIN com_pro_ratecard_rates AS rt
ON (dr.movie_id=rt.movie_id and
dr.theater_id=rt.theater_id and
dr.showtime_id=rt.showtime_id and
dr.category_id=rt.category_id and
dr.applied_date = rt.date_apply)
INNER JOIN com_pro_ratecard as rc ON (rc.id=rt.ratecard_id)
INNER JOIN com_pro_movie as m ON (m.id=dr.movie_id)
INNER JOIN com_pro_theater as t ON (t.id=dr.theater_id)
INNER JOIN com_pro_share as st ON (st.id=rc.share_id)
INNER JOIN com_pro_theater_dr as td ON (td.theater_id=dr.theater_id)
INNER JOIN com_pro_circuit as c ON (c.id =td.circuit_id)
WHERE 1
You can always do something like this:
SELECT SUM(val)
FROM
(
SELECT (SUM(dr.drv)/100)*
(st.suppliers1_value+st.suppliers2_value+st.suppliers3_value+st.suppliers4_value)
AS val
/* the rest of your current query */
) AS a

SQL query for returning multiple fields joined to the same reference

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'