Please help on this query, here is sql fiddler : http://sqlfiddle.com/#!2/8acc1/1
SELECT DISTINCT c.id, v.sel_category,c.curr_tittle , c.curr_desc, v.videos_desc
FROM wp_curriculum c, wp_career_vidoes v
WHERE c.id IN
(SELECT DISTINCT curr_id
FROM wp_curriculum_category
WHERE curr_category IN (2,3)) AS I
AND I.curr_category = v.sel_category
GROUP BY I.curr_category
do you mean this?
SELECT DISTINCT c.id,
v.sel_category,
c.curr_tittle ,
c.curr_desc,
v.videos_desc
FROM wp_curriculum c
INNER JOIN wp_curriculum_category cat
ON c.id = cat.curr_id
INNER JOIN wp_career_vidoes v
ON v.sel_category = cat.curr_category
WHERE cat.curr_category IN (2,3)
SQLFiddle Demo
To further gain more knowledge about joins, visit the link below:
Visual Representation of SQL Joins
Related
I would like to optimise the query I have below:
SELECT
a.id, a.pay_point_name, a.pay_point_location,
COUNT(b.id) AS countedCustomers,
SUM(b.approved) AS summedLoans,
SUM(c.deduction) AS summedDeductions
FROM
pay_tbl a
LEFT JOIN
customer_tbl b
ON b.employer = a.pay_point_name
LEFT JOIN
loans_tbl c
ON c.paypoint = a.pay_point_name
GROUP BY
a.pay_point_name
ORDER BY
NULL
Current Execution time: 161.2s
EXPLAIN statement gives me the table below:
I would like to know how best to optimise this query and reduce execution time.
Please check this one where I've used subquery. If this works better create view with this query.
-- MySQL
SELECT t.id, t.pay_point_name
, t.pay_point_location
, COALESCE(t.countedCustomers, 0) countedCustomers
, COALESCE(t.summedLoans, 0) summedLoans
, COALESCE(p.summedDeductions, 0) summedDeductions
FROM (SELECT a.id, a.pay_point_name
, MAX(a.pay_point_location) pay_point_location
, COUNT(b.id) AS countedCustomers
, SUM(b.approved) AS summedLoans
FROM pay_tbl a
LEFT JOIN customer_tbl b
ON b.employer = a.pay_point_name
GROUP BY a.id, a.pay_point_name) t
LEFT JOIN (SELECT paypoint
, SUM(deduction) AS summedDeductions
FROM loans_tbl
GROUP BY paypoint) p
ON t.pay_point_name = p.paypoint
ORDER BY t.id;
Suppose if you have to repeatedly execute the same query frequently. You can use MySql views.
https://www.mysqltutorial.org/mysql-views-tutorial.aspx
I am working on migrating from Oracle to MySQL and currently stuck on converting query with MINUS operation. I know, I have to use left join, but somehow my query is not giving the exact rows as it is in Oracle. The query is very simple with with select on views "V_*".
On Oracle:
select s.section, c.title from course c, v_staff_active s
minus
select section,title from v_rep_attended_by_section
On Mysql, I converted it like below:
select * from
( select s.section, c.title from course c, v_staff_active s) x
left join
( select section,title from v_rep_attended_by_section) y on x.section = y.section
where y.section is null;
But not getting the exact result or number of records.
Can you please help me out as I am very much new to MySQL.
Thanks in advance.
Your query should look like:
select s.section, c.title
from course c
left join v_staff_active s on c.section = s.section
and c.title = s.title
where s.section is null and s.title is null;
I am not sure if my method is possible, but i'm trying to do a group_concat on a select statement that concats 2 fields. I get the error: Subquery returns more than 1 row each time. Can anyone help me as to a solution, or better way around this.
select t.recnum, (select group_concat((select concat(b.origtests,'^', d.name) as testing
from order_origtests b
join profile c on c.code = b.origtests
join department d on d.recnum = c.dept
)))
FROM order_ t
You don't put SELECT inside GROUP_CONCAT. It should be
select t.recnum, (
select group_concat(concat(b.origtests,'^', d.name))
from order_origtests b
join profile c on c.code = b.origtests
join department d on d.recnum = c.dept
) AS testing
FROM order_ t
Note that your subquery isn't correlated to anything in t, so you'll get the same testing column for every recnum.
I need to do a correlated SQL Query and for that purpose i need to provide an alias to outer query which in which I perform an inner join. I am not able to do the alias
SELECT DISTINCT(name)
FROM PERSON
INNER JOIN M_DIRECTOR AS dira
ON (dira.PID = M_DIRECTOR.PID) as dira
WHERE 9 > (
SELECT COUNT(MID) FROM M_DIRECTOR WHERE name = dira.name
) ;
I didn't really understand what you want to do, but I guess
select
distinct p.name,
count(d.MID) cnt
from
hindi2_PERSON p
inner join
hindi2_M_DIRECTOR d
on
p.PID = d.PID
group by
p.name
having count(d.MID) > 9
;
would do what you want
I dont know what you are asking and what you mean by make an alias to an eniter result ?
but you are doing
select distinct(name) as othername
which is you are selecting name and you are giving here othername as an alias
then you retrieve it in result
$row['othername']
There's still something missing. From what you write, there is a field name in the M_DIRECTOR table?
Please show all the tables and attributes involved, use an SQL Fiddle to prepare an example.
SELECT DISTINCT(name)
FROM PERSON as p
INNER JOIN (
SELECT COUNT(MID), PID FROM M_DIRECTOR WHERE name = dira.name
) as d
ON (p.PID = d.PID) ;
I've created sqlfiddle to try and get my head around this http://sqlfiddle.com/#!2/21e72/1
In the query, I have put a max() on the compiled_date column but the recommendation column is still coming through incorrect - I'm assuming that a select statement will need to be inserted on line 3 somehow?
I've tried the examples provided by the commenters below but I think I just need to understand this from a basic query to begin with.
As others have pointed out, the issue is that some of the select columns are neither aggregated nor used in the group by clause. Most DBMSs won't allow this at all, but MySQL is a little relaxed on some of the standards...
So, you need to first find the max(compiled_date) for each case, then find the recommendation that goes with it.
select r.case_number, r.compiled_date, r.recommendation
from reporting r
join (
SELECT case_number, max(compiled_date) as lastDate
from reporting
group by case_number
) s on r.case_number=s.case_number
and r.compiled_date=s.lastDate
Thank you for providing sqlFiddle. But only reporting data is given. we highly appreciate if you give us sample data of whole tables.
Anyway, Could you try this?
SELECT
`case`.number,
staff.staff_name AS ``case` owner`,
client.client_name,
`case`.address,
x.mx_date,
report.recommendation
FROM
`case` INNER JOIN (
SELECT case_number, MAX(compiled_date) as mx_date
FROM report
GROUP BY case_number
) x ON x.case_number = `case`.number
INNER JOIN report ON x.case_number = report.case_number AND report.compiled_date = x.mx_date
INNER JOIN client ON `case`.client_number = client.client_number
INNER JOIN staff ON `case`.staff_number = staff.staff_number
WHERE
`case`.active = 1
AND staff.staff_name = 'bob'
ORDER BY
`case`.number ASC;
Check below query:
SELECT c.number, s.staff_name AS `case owner`, cl.client_name,
c.address, MAX(r.compiled_date), r.recommendation
FROM case c
INNER JOIN (SELECT r.case_number, r.compiled_date, r.recommendation
FROM report r ORDER BY r.case_number, r.compiled_date DESC
) r ON r.case_number = c.number
INNER JOIN client cl ON c.client_number = cl.client_number
INNER JOIN staff s ON c.staff_number = s.staff_number
WHERE c.active = 1 AND s.staff_name = 'bob'
GROUP BY c.number
ORDER BY c.number ASC
SELECT
case.number,
staff.staff_name AS `case owner`,
client.client_name,
case.address,
(select MAX(compiled_date)from report where case_number=case.number),
report.recommendation
FROM
case
INNER JOIN report ON report.case_number = case.number
INNER JOIN client ON case.client_number = client.client_number
INNER JOIN staff ON case.staff_number = staff.staff_number
WHERE
case.active = 1 AND
staff.staff_name = 'bob'
GROUP BY
case.number
ORDER BY
case.number ASC
try this