Can we use OR in a mysql inner join - mysql

SELECT DISTINCT (
A.`id`
), A.`id` , A.`no` , B.amount, SUM( A.`outward` * A.`price` ) AS total, A.`outward_date`
FROM `outward` A
INNER JOIN franchisees B
INNER JOIN store C
INNER JOIN shoppe D
WHERE B.user_id = C.user_id
AND (C.pos_id = A.no)//(C.pos_id = A.no OR (D.id = A.no)
OR (D.id = A.no)
AND A.outward_date = '2012-02-10'
GROUP BY A.req_id
ORDER BY A.no ASC , A.`d` ASC , B.amount ASC
The problem in this query is that the SUM( A.outward * A.price ) AS total comes differently which is not related to
ouptut
id sum(outward * price)
12021030738-105 485.220000000000
1202104186-104 2504.410000000000
output displayed
12021030738-105 32557.33
1202104186-104 6307.86
i guess the problem is with the OR statement? can anyone find the issue with the query

Try below. enclose OR Conditions in a parenthesis:
SELECT DISTINCT (
A.`id`
), A.`id` , A.`no` , B.amount, SUM( A.`outward` * A.`price` ) AS total, A.`outward_date`
FROM `outward` A
INNER JOIN franchisees B
INNER JOIN store C
INNER JOIN shoppe D
WHERE B.user_id = C.user_id
AND ((C.pos_id = A.no) OR (D.id = A.no))
AND A.outward_date = '2012-02-10'
GROUP BY A.req_id
ORDER BY A.no ASC , A.`d` ASC , B.amount ASC

Related

how to sum using distinct in mysql

i want to sum a total value of eligible_2018 with distinct of ecode_id
select distinct(A.ecode_id),C.eligible_2018 AS monthlyBudgetAmt from vefm_dailybudget AS A JOIN vefm_comp_division AS B ON (B.id = A.division)
JOIN vefm_engineer_details AS C ON (A.ecode_id=C.ecode)
LEFT JOIN vefm_user AS F ON (A.approved_by=F.ecode)
JOIN vefm_region AS D ON (A.region=D.id)
where A.approval_amount != 0 AND A.division=1 and date(A.approval_date) BETWEEN '2018-04-01' AND '2018-04-30'
AND A.budget_type='monthly' GROUP BY A.ecode_id
The distinct A.ecode_id are directly managed by group by
SELECT A.ecode_id
, SUM(C.eligible_2018) monthlyBudgetAmt
FROM vefm_dailybudget A
JOIN vefm_comp_division B ON B.id = A.division
JOIN vefm_engineer_details C ON A.ecode_id=C.ecode
LEFT JOIN vefm_user F ON A.approved_by=F.ecode
JOIN vefm_region D ON A.region=D.id
WHERE A.approval_amount != 0
AND A.division=1
AND date(A.approval_date)
BETWEEN '2018-04-01' AND '2018-04-30'
AND A.budget_type='monthly'
GROUP BY A.ecode_id
if you need the overall sum for distinct result then you can try
SELECT SUM( t.monthlyBudgetAmt )
FROM (
SELECT DISTINCT A.ecode_id
,C.eligible_2018 AS monthlyBudgetAmt
FROM vefm_dailybudget AS A
JOIN vefm_comp_division AS B ON (B.id = A.division)
JOIN vefm_engineer_details AS C ON (A.ecode_id=C.ecode)
LEFT JOIN vefm_user AS F ON (A.approved_by=F.ecode)
JOIN vefm_region AS D ON (A.region=D.id)
WHERE A.approval_amount != 0
AND A.division=1
AND date(A.approval_date) BETWEEN '2018-04-01' AND '2018-04-30'
AND A.budget_type='monthly' GROUP BY A.ecode_id
)

mysql query taking long time to respond

SELECT t.id
, t.department
, t.owner
, t.client
, u.username as owner_name
, c.name as catagery
, d.dept_name as deptname
, t.periority
, t.status
, t.estimate
, cl.takeaway_name
from tbl_task t
JOIN tbl_user u
ON u.id = t.owner
JOIN tbl_task_catagery c
ON c.id = t.catagery
JOIN tbl_department d
ON d.id = t.department
JOIN tbl_clients cl
ON cl.id = t.client
and t.status = 0
and (t.id in (select task_id
from tbl_task_note tn
where tn.user_id = '69'
and tn.id in (select max(id)
from tbl_task_note tt
where tt.task_id = tn.task_id
)
)
)
order by t.id
Note : The above query is used for check users hold tasks. tbl_task_note table is used for check task notes for separate users task.
With this query you will get the task that have the last task_note registered, including the user, departament, client, and some other.
If it is what you need you can just do this.
select
t.id,
t.department,
t.owner,
t.client,
u.username as owner_name,
c.name as catagery,
d.dept_name as ptname,
t.periority,
t.status,
t.estimate,
cl.takeaway_name
from tbl_task t
INNER JOIN tbl_user u ON u.id=t.owner
INNER JOIN tbl_task_catagery c ON c.id=t.catagery
INNER JOIN tbl_department d ON d.id=t.department
INNER JOIN tbl_clients cl ON cl.id=t.client and t.status=0
INNER JOIN (select * from tbl_task_note where id =
(select max(id) from tbl_task_note)
)tb on tb.task_id = t.id
order by t.id
That way you can improve your query.
You shoud also ensure that your keys compared are foreign keys to get faster consults.

Error in MySQL Query with multiple joins and limit on first table

so i had to adjust a query i used because the limit at the end of the table would cause the entire first table to be read before limiting. this resulted in a timeout from my mysql server. now i created the query like i read in another post on stack overflow and came up with this:
SELECT a.title
, a.lat
, a.lon
, a.a_content_id
, a.date_added
, r.countRep
, i.countInt
, content.img
, c.marker
, c.subcatvan
FROM
( SELECT title
, lat
, lon
, alert_content_id
, date_added
, cat
FROM alerts
LIMIT 10
) a
LEFT
JOIN
( SELECT COUNT(DISTINCT id) countRep
FROM reply
WHERE alert_id = alerts.alerts
) r
LEFT
JOIN
( SELECT COUNT(DISTINCT id) countInt
FROM interactions
WHERE alert_id = alerts.alerts
) i
LEFT
JOIN
( SELECT img
FROM alerts_content
WHERE alert_id = alerts.alerts
) content
LEFT
JOIN
( SELECT marker
, subcatvan
FROM categories
WHERE a.cat = id
) c;
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 45
This was my original query which resulted in a timeout:
SELECT
a.title, a.lat, a.lon, a.alert_content_id, a.date_added, count(DISTINCT r.id) as countRep ,count(DISTINCT i.id) AS countInt,
ac.img, c.marker, c.subcatvan
FROM `alerts` a
LEFT JOIN
`reply` r ON r.alert_id = a.alerts
LEFT JOIN
`interactions` i ON i.alert_id = a.alerts
LEFT JOIN
`alerts_content` ac ON ac.alert_id = a.alerts
LEFT JOIN
`categories` c ON a.cat = c.id
GROUP BY a.title, a.lat, a.lon, a.alert_content_id, a.date_added LIMIT 0,10
Does anyone know what is causing the error?
Or someone that knows how to correct my original query?
You need to fix the subqueries. They need to use group by rather than be correlated:
SELECT a.title, a.lat, a.lon, a.a_content_id, a.date_added, r.countRep,
i.countInt, content.img, c.marker, c.subcatvan
FROM (SELECT title, lat, lon, alert_content_id, date_added, cat
FROM `alerts`
LIMIT 10
) a LEFT JOIN
(SELECT alert_id, count(DISTINCT id) as countRep
FROM `reply`
GROUP BY alert_id
) r
ON r.alert_id = a.alerts r LEFT JOIN
(SELECT alert_id, count(DISTINCT id) AS countInt
FROM `interactions`
GROUP BY alert_id
) i
ON i alert_id = a.alerts LEFT JOIN
(SELECT alert_id, img
FROM `alerts_content`
GROUP BY alert_id
) content
ON alert_id = a.alerts LEFT JOIN
`categories` c
ON a.cat = c.id;
Try adding the table name to the id, in the where.
c.id instead of id
Fixed it thanks to #Ravinder his comment about missing the on clause
updated sql:
SELECT
a.alerts, a.title, a.lat, a.lon, a.alert_content_id, a.date_added, r.countRep, i.countInt,
ac.img, c.marker, c.subcatvan
FROM
(SELECT alerts, title, lat, lon, alert_content_id, date_added, cat
FROM `alerts` LIMIT 10) a
LEFT JOIN
(SELECT alert_id,count(DISTINCT id) as countRep
FROM `reply`) r
ON r.alert_id = a.alerts
LEFT JOIN
(SELECT alert_id,count(DISTINCT id) AS countInt
FROM `interactions`) i
ON i.alert_id = a.alerts
LEFT JOIN
(SELECT alert_id, img
FROM `alerts_content`) ac
ON ac.alert_id = a.alerts
LEFT JOIN
(SELECT id, marker, subcatvan
FROM `categories`) c
ON a.cat = c.id
GROUP BY a.title, a.lat, a.lon, a.alert_content_id, a.date_added

MySQL query not working with another database

I have this query which is working fine:
SELECT c.id, c.name
FROM job j
LEFT JOIN portfolio_job_category pjc ON pjc.job_id = j.id
LEFT JOIN category c ON c.id = pjc.category_id
WHERE j.end_date >= DATE( NOW( ) )
AND pjc.portfolio_id =3
GROUP BY c.id
ORDER BY `c`.`id` ASC
LIMIT 0 , 30
But when I do query below it doesn't work and returns empty rows:
SELECT c.id, c.name
FROM commstratjobs.job j
LEFT JOIN commstratjobs.portfolio_job_category pjc ON pjc.job_id = j.id
LEFT JOIN commstratjobs.category c ON c.id = pjc.category_id
WHERE j.end_date >= DATE( NOW( ) )
AND pjc.portfolio_id =3
GROUP BY c.id
ORDER BY c.name;
LIMIT 0 , 30
Is there a reason why?
Only thing I did was to add database in front of tables...
did you try to seprate database & table by this symbol : `
i.e
commstratjobs.job
so your query will be like this
SELECT c.id, c.name
FROM `commstratjobs`.`job j`
LEFT JOIN `commstratjobs`.`portfolio_job_category` pjc ON pjc.job_id = j.id
LEFT JOIN `commstratjobs`.`category` c ON c.id = pjc.category_id
WHERE j.end_date >= DATE( NOW( ) )
AND pjc.portfolio_id =3
GROUP BY c.id
ORDER BY c.name;
LIMIT 0 , 30

mysql, use two select and order by, new value

SELECT a.id, a.name, a.ad, c.name, c.phone, c.email,
(
SELECT b.id_user
FROM price_b b
WHERE b.id = a.id
ORDER BY b.date DESC
LIMIT 1
) AS f_user_id
FROM a_emtp a
LEFT JOIN customer c ON
c.id = f_user_id
WHERE a.show = "1"
Hi, why show this error: Unknown column 'f_user_id' in 'on clause'
Thanks
SELECT a.id, a.name, a.ad, c.name, c.phone, c.email,
(
SELECT b.id_user
as f_user_id
FROM price_b b
WHERE b.id = a.id
ORDER BY b.date DESC
LIMIT 1
)
FROM a_emtp a
LEFT JOIN customer c ON
c.id = f_user_id
WHERE a.show = "1"
You can reference labelled fields from subqueries inside outer queries. You could even drop the relabelling and just use "b.id_user".
Probably a comma after c.email was missing.
SELECT a.id, a.name, a.ad, c.name, c.phone, c.email,
(
SELECT b.id_user
FROM price_b b
WHERE b.id = a.id
ORDER BY b.date DESC
LIMIT 1
) AS f_user_id
FROM a_emtp a
LEFT JOIN customer c ON
c.id = f_user_id
WHERE a.show = "1"