Mysql query with multiple joins and conditions - mysql

I am trying to connect four tables using joints,here i used left join to connect tables and my condition is all the goods,item should be same and all the site should be same.same site have multiple goods, so i want to get the sum number of goods from each table. my query is given
select
a.goods
,sum(a.no_of_units) as totala
, a.site
,b.item
,sum(b.quantity) as totalb
,b.site
,c.goods
,c.site
,sum(c.no_of_units) as totalc
,d.site
,d.goods
,sum(d.quantity)b as totald
from
inward_stock a
left join
opening_balance b
on
a.site=b.site
and
a.goods=b.item
left join
return_stock c
on
b.site=c.site
and
b.item=c.goods
left join
stock_consumed d
on
d.site=c.site
and
d.goods=c.goods

Can you put your conditions at the end of the joints like this:
select a.goods,sum(a.no_of_units) as totala, a.site,b.item,sum(b.quantity) as totalb,b.site,c.goods,c.site,sum(c.no_of_units) as totalc,d.site,d.goods,sum(d.quantity) as totald
from inward_stock a left join
opening_balance b on (a.site=b.site) left join
return_stock c on (b.site=c.site) left join
stock_consumed d on (d.site=c.site) where (a.goods=b.item) and (b.item=c.goods) and (d.goods=c.goods)
I have not tested your request but it seems that it's not bad.

Related

no code error in my mysql query yet no results

CREATE TABLE wreport13
AS
SELECT customers.CName, customers.CIDCard, customers.Cphone, customers.infection, users.UName, authlist.AuthType, checkup.comment
FROM ((((checkup
INNER JOIN customers ON customers.CID=checkup.CID)
INNER JOIN users ON users.UID=checkup.EID)
INNER JOIN uauth ON users.UID=uauth.UID)
INNER JOIN authlist ON authlist.AID=uauth.AuthID)
WHERE (checkup.Result=1)
and (checkup.QID=9)
and (checkup.cdate='2020-05-23')
and (uauth.astatus=1)
this query should result 4 records but nothing comes out.
please help me to find the error,
when i used this below query
SELECT cdate,COUNT(CID) AS customers
FROM (checkup
INNER JOIN questionaire ON questionaire.QID=checkup.QID)
WHERE checkup.Result=1 and checkup.QID=9
GROUP BY cdate
and result is
cdate customers
2020-05-23 4
2020-05-25 1
2020-05-30 3
2020-05-31 2
the first query is meant to extract the details of the first row of the above query
Sorry, it was a mistake in inner join key with customers table
SELECT customers.CName, customers.CIDCard, customers.Cphone, customers.infection,
users.UName, authlist.AuthType, checkup.comment
FROM ((((checkup
INNER JOIN customers ON customers.CID=checkup.CuID)
INNER JOIN users ON users.UID=checkup.EID)
INNER JOIN uauth ON users.UID=uauth.UID)
INNER JOIN authlist ON authlist.AID=uauth.AuthID)
WHERE (checkup.Result=1)
and (checkup.QID=9)
and (checkup.cdate='2020-05-23')
and (uauth.astatus=1)

Unable to get complete data using mysql 3 joins query

i have 1500 records in domains table but by using this query only get 1215 records. How to modify this query to give desired outcome and better performance
SELECT
d.id, d.domain_name, d.action, d.comment, d.agent_email,
d.assigned_date, d.added_date, dd.registered_on, dd.expiry_date,
dd.updated_date, dd.acquire_price, dd.acquire_date, dd.email,
dd.effective_price, dd.registrar, dd.status, dd.servers,
count(l.lead_domain), d.domainer_email, d.current_status,
d.undeveloped, d.sedo, d.afternic, d.flippa, d.uniregistry,
d.go_daddy, d.domr, d.minimum_offer, d.buy_it_now_price,
d.way_to_find_leads, dd.tlds_taken
FROM domains d
right join domains_data dd on d.domain_name=dd.domain_name
left outer join lead_domains l on d.domain_name=l.domain_name
and d.domainer_email=l.domainer_email
group by d.domain_name
having d.action='all' and d.domainer_email='abc#gmail.com'
order by d.added_date desc;
Your results depend on the domains_data table.
Since you are right joining domains to domains_data, the entries that are in domains_data are taken. If you want the entries of domains to be considered, use a left join like below.
SELECT d.id, d.domain_name, d.action, d.comment, d.agent_email, d.assigned_date, d.added_date, dd.registered_on, dd.expiry_date, dd.updated_date, dd.acquire_price, dd.acquire_date, dd.email, dd.effective_price, dd.registrar, dd.status, dd.servers, COUNT(l.lead_domain), d.domainer_email, d.current_status, d.undeveloped, d.sedo, d.afternic, d.flippa, d.uniregistry, d.go_daddy, d.domr, d.minimum_offer, d.buy_it_now_price, d.way_to_find_leads, dd.tlds_taken
FROM domains d
LEFT JOIN domains_data dd ON d.domain_name=dd.domain_name
LEFT OUTER JOIN lead_domains l ON d.domain_name=l.domain_name AND d.domainer_email=l.domainer_email
GROUP BY d.domain_name
HAVING d.action='all' AND d.domainer_email='abc#gmail.com'
ORDER BY d.added_date DESC;
If you are still not able to get the desired results, check the having conditions.
#Harshal As per my understanding, I don't think that query you have provided will work as GROUP BY rules are not followed properly.
SELECT d.id,
d.domain_name,
d.action,
d.comment,
d.agent_email,
d.assigned_date,
d.added_date,
dd.registered_on,
dd.expiry_date,
dd.updated_date,
dd.acquire_price,
dd.acquire_date,
dd.email,
dd.effective_price,
dd.registrar,
dd.status,
dd.servers,
(SELECT COUNT(lead_domain) FROM lead_domains WHERE domain_name = d.domain_name AND domainer_email = d.domainer_email) AS lead_domain,
d.domainer_email,
d.current_status,
d.undeveloped,
d.sedo,
d.afternic,
d.flippa,
d.uniregistry,
d.go_daddy,
d.domr,
d.minimum_offer,
d.buy_it_now_price,
d.way_to_find_leads,
dd.tlds_taken
FROM domains d
LEFT JOIN domains_data dd
ON d.domain_name = dd.domain_name
WHERE d.action = 'all'
AND d.domainer_email = 'abc#gmail.com'
ORDER BY
d.added_date DESC;
And as you have not provided any expected results, I am just guessing that my solution will work.
Here's my suggestion, filter first before joining other tables.
SELECT d.id
,d.domain_name
,d.action
,d.comment
,d.agent_email
,d.assigned_date
,d.added_date
,dd.registered_on
,dd.expiry_date
,dd.updated_date
,dd.acquire_price
,dd.acquire_date
,dd.email
,dd.effective_price
,dd.registrar
,dd.status
,dd.servers
,t1.cnt
,d.domainer_email
,d.current_status
,d.undeveloped
,d.sedo
,d.afternic
,d.flippa
,d.uniregistry
,d.go_daddy
,d.domr
,d.minimum_offer
,d.buy_it_now_price
,d.way_to_find_leads
,dd.tlds_taken
FROM domains d
inner join
(select domain_name, count(1) as cnt
from domain
where
action='all' and domainer_email='abc#gmail.com'
group by domain_name
) as t1 on t1.domain_name = d.domain_name
right join domains_data dd on d.domain_name=dd.domain_name
left outer join lead_domains l on d.domain_name=l.domain_name and d.domainer_email=l.domainer_email
order by d.added_date desc;

MySQL INNER JOIN Multiple columns

I have two tables
Table1: violations
Columns: date, time, pdid, pname, v1, v2, v3, v4
Each v1 through v4 has an integer value which corresponds to a single entry (ID) in table 2.
table2: parking_violations
columns: code, section, description, ID
I need to query each record of violations based on pdid, and match each 'v1-v4' to column 'ID' in the p_violations table.
SELECT
parking.date,
parking.time,
parking.pname,
parking_violations.code,
parking_violations.section,
parking_violations.description
FROM
parking
INNER JOIN parking_violations ON parking.v1=parking_violations.ID
WHERE
pdid=5
This returns the correct records for V1, but I cannot figure out how to also return V2-V4 all populated by matching the value to ID.
Like #fa06 explain, you can use multiple joins to the same table, but instead of inner join i will use left join, this way i have the flexibility of get rows where not all vN have matching IDs on the table parking_violations.
SELECT
parking.date,
parking.time,
parking.pname,
pv1.code,
pv1.section,
pv1.description,
pv2.code,
pv2.section,
pv2.description,
pv3.code,
pv3.section,
pv3.description,
pv4.code,
pv4.section,
pv4.description
FROM
parking
LEFT JOIN
parking_violations AS pv1 ON pv1.ID = parking.v1
LEFT JOIN
parking_violations AS pv2 ON pv2.ID = parking.v2
LEFT JOIN
parking_violations AS pv3 ON pv3.ID = parking.v3
LEFT JOIN
parking_violations AS pv4 ON pv4.ID = parking.v4
WHERE
parking.pdid = 5;
use join multiple time with alias like below:
SELECT
parking.date,
parking.time,
parking.pname,
parking_violations.code,
parking_violations.section,
parking_violations.description
FROM
parking
INNER JOIN parking_violations ON parking.v1=parking_violations.ID
inner join parking_violations a ON parking.v2=a.ID
inner join parking_violations b ON parking.v3=b.ID
inner join parking_violations c ON parking.v4=d.ID
WHERE
pdid=5

Join 4 tables with specified column

I have this code:
SELECT A.UNITCODE, B.FORMATIONCODE, C.UPPERFORMATIONCODE, D.UPPERFORMATIONCODE
FROM UNIT AS A.UNITCODE
INNER JOIN FORMATION AS B.FORMATIONCODE
INNER JOIN UPPERFORMATION_UNIT AS C.UPPERFORMATION
INNER JOIN UPPERFORMATION AS D.UPPERFORMATIONCODE
WHERE UNITCODE='7000007'
Can you guys help me how to join 4 tables with specified column?
Assuiming the all 3 related tables have the same UNIT_ID for join with table UNIT
SELECT
A.UNITCODE
, B.FORMATIONCODE
, C.UPPERFORMATIONCODE
, D.UPPERFORMATIONCODE
FROM UNIT AS A
INNER JOIN FORMATION AS B ON B.FORMATIONCODE = A.UNIT_ID
INNER JOIN UPPERFORMATION_UNIT AS C. C.UPPERFORMATION = A.UNIT_ID
INNER JOIN UPPERFORMATION AS D D.UPPERFORMATIONCODE = A.UNIT_ID
WHERE UNITCODE='7000007'
It seems you are confusing table aliases and linking columns.
This is how to give a table an alias name in the query in order to enhance readability:
INNER JOIN formation AS f
where the AS is optional. Most often it is ommited.
This is how to join:
FROM unit AS u
INNER JOIN upperformation_unit AS ufu ON ufu.unitcode = u.unitcode
Well, I don't know the columns linking the tables of course, let alone their names. But I suppose the query would have to look like this more or less:
SELECT
u.unitcode,
f.formationcode,
uf.upperformationcode,
ufu.upperformationcode
FROM unit u
JOIN upperformation_unit ufu ON ufu.unitcode = u.unitcode
JOIN upperformation uf ON uf.upperformationcode = ufu.upperformationcode
JOIN formation f ON f.formationcode uf.formationcode
WHERE u.unitcode = 7000007;

MySQL how to select what I need, most likely an inner join

A bit of a newbie question, probably an INNER JOIN with an "AS" statement, but I can't figure it out...
This is for a MYSQL based competition app. I want to select the "img_title" for both img_id1 and img_id2. I can't figure out how to do it and still see which title is assigned to the associated _id1 or _id2.
My tables:
competitions
comp_id
img_id1
img_id2
on_deck
img_id
img_title
Desired results:
comp_id | img_id1 | img_title1 |img_id2 | img_title2
You need a join for each image:
SELECT comp.comp_id, img1.img_id, img1.img_title, img2.img_id, img2.img_title
FROM competitions comp
INNER JOIN on_deck img1 ON img1.img_id = comp.img_id1
INNER JOIN on_deck img2 ON img2.img_id = comp.img_id2
LEFT JOIN if img_id1 or img_id2 can be NULL.
select comp_id, img_id1, b.img_title as img_title1,
img_id2, b2.img_title as img_title2
from competitions a
left outer join on_deck b on b.img_id = a.img_id1
left outer join on_deck b2 on b2.img_id = a.img_id2
switch let outer join to inner join if you want to exclude rows in competitions that do not have two matching img_ids
This query should give you the results you want. This also assumes that comp.img_id1 and comp.img_id2 are never NULL.
SELECT comp.comp_id
, comp.img_id1
, deck1.img_title AS img_title1
, comp.img_id2
, deck2.img_title AS img_title2
FROM competitions AS comp
JOIN on_deck deck1 ON comp.img_id1 = deck1.img_id
JOIN on_deck deck2 ON comp.img_id2 = deck2.img_id
If you have plan on having a NULL or empty string comp.img_id1 and/or comp.img_id2 fields, you'll need to do some left joins.