I am attempting to create a stored procedure but every time I try to apply it won't let me and throws the following error: Error Code: 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 ' CREATE DEFINER=Admin#% PROCEDURE culls_&_deactivation() BEGIN D' at line 1
I have tried everything including running each part of the procedure and they all work. But sometimes the insert will fail and sometimes the create fails so I am not entirely sure what the issues are.
Also for reference I am using MySql 8.0.17
DELIMITER $$
CREATE DEFINER=`Admin`#`%` PROCEDURE `culls_and_deactivations`()
BEGIN
DROP TABLE IF EXISTS `metrics`.`tempvault`;
CREATE TABLE `metrics`.`tempvault`(
vault_date DATE DEFAULT NULL
, matterid varchar(32) DEFAULT NULL
, MatterName TEXT
, AccountName TEXT
, owner_REP TEXT
, valt TEXT
, mrr decimal(10,2) DEFAULT NULL
, GbHosted float DEFAULT NULL
, company_profile__c TEXT
, week_start_date DATE DEFAULT NULL
, week_end_date DATE DEFAULT NULL
, month_and_year varchar(30) default NULL
);
INSERT INTO metrics.tempvault
SELECT
CAST(scsdr.begin_date__c AS date)
, m.id
, m.Name AS MatterName
, a.Name AS AccountName
, CONCAT(u.firstname, ' ', u.lastname) AS owner
, "VAULT" AS Type
, NULL AS mrr
, dgh.GbHosted AS GbHosted
, a.company_profile__c
, du.week_start_date
, du.week_end_date
, du.month_and_year
FROM metrics.sf_matter__c m
INNER JOIN metrics.sf_cold_storage_date_range__c scsdr
ON scsdr.matter__c = m.id
INNER JOIN metrics.daily_gb_hosted dgh
ON dgh.matterid = m.id
#AND dgh.date_value = CAST(NOW() AS DATE)
AND dgh.date_value = DATE_ADD(CAST(scsdr.begin_date__c AS DATE), INTERVAL -1 DAY)
INNER JOIN metrics.daily_matter_contract_mapping dmcm
ON dmcm.matterid = m.id
AND dmcm.date_value = CAST(scsdr.begin_date__c AS DATE)
INNER JOIN metrics.sf_contract c
ON c.id = dmcm.contractid
INNER JOIN date_utility du
ON du.date_value = CAST(scsdr.begin_date__c AS DATE)
INNER JOIN metrics.sf_account a
ON a.id = c.billingaccount__c
LEFT JOIN metrics.sf_sf_user u
ON u.id = m.salesperson__c
WHERE 1=1
AND CAST(scsdr.begin_date__c AS date) >= '2018-01-01'
AND c.type__c = 'Transactional';
SELECT * FROM (
SELECT
CAST(m.deactivationdate__c AS DATE) AS Date
, m.id AS matterid
, m.Name AS MatterName
, a.Name AS AccountName
, CONCAT(firstname, ' ', lastname) AS owner
, "DEACTIVATION" AS Type
, mmmts.mrr AS mrr
, dgh.GbHosted AS GbHosted
, a.company_profile__c
, du.week_start_date
, du.week_end_date
, du.month_and_year
FROM metrics.sf_matter__c m
INNER JOIN metrics.daily_gb_hosted dgh
ON dgh.matterid = m.id
#AND dgh.date_value = CAST(NOW() AS DATE)
AND dgh.date_value = DATE_ADD(CAST(m.deactivationdate__c AS DATE), INTERVAL -1 DAY)
INNER JOIN metrics.daily_matter_contract_mapping dmcm
ON dmcm.matterid = m.id
#AND dmcm.date_value = CAST(NOW() AS DATE)
AND dmcm.date_value = CAST(m.deactivationdate__c AS DATE)
INNER JOIN metrics.sf_contract c
ON c.id = dmcm.contractid
INNER JOIN date_utility du
ON du.date_value = CAST(m.deactivationdate__c AS DATE)
INNER JOIN metrics.sf_account a
ON a.id = c.billingaccount__c
LEFT JOIN metrics.sf_sf_user u
ON u.id = m.salesperson__c
LEFT JOIN metrics.matter_monthly_mrr_time_series mmmts
ON mmmts.matter_id = m.id
AND mmmts.date_value = du.month_end_date
WHERE 1=1
AND CAST(m.deactivationdate__c AS DATE) >= '2018-01-01'
#AND gbHosted > 10
AND c.type__c = 'Transactional'
GROUP BY CAST(m.deactivationdate__c AS DATE), m.Name, a.Name
) AS t1
UNION
SELECT * FROM (
SELECT
MIN(CAST(d.dateadded__c AS DATE)) AS Date
, m.id AS matterid
, m.Name AS MatterName
, a.Name AS AccountName
, CONCAT(firstname, ' ', lastname) AS owner
, "CULL" AS Type
, mmmts.mrr AS mrr
, MAX(dgh.GbHosted) AS GbHosted
, a.company_profile__c
, du.week_start_date
, du.week_end_date
, du.month_and_year
FROM metrics.sf_matter__c m
INNER JOIN metrics.sf_dataset__c d
ON m.id = d.matter__c
AND d.data_size__c < 0
INNER JOIN metrics.daily_gb_hosted dgh
ON dgh.matterid = m.id
AND dgh.date_value = DATE_ADD(CAST(d.dateadded__c AS DATE), INTERVAL -1 DAY)
INNER JOIN metrics.daily_matter_contract_mapping dmcm
ON dmcm.matterid = m.id
AND dmcm.date_value = CAST(d.dateadded__c AS DATE)
INNER JOIN metrics.sf_contract c
ON c.id = dmcm.contractid
INNER JOIN date_utility du
ON du.date_value = CAST(d.dateadded__c AS DATE)
INNER JOIN metrics.sf_account a
ON a.id = c.billingaccount__c
LEFT JOIN metrics.sf_sf_user u
ON u.id = m.salesperson__c
LEFT JOIN metrics.matter_monthly_mrr_time_series mmmts
ON mmmts.matter_id = m.id
AND mmmts.date_value = du.month_end_date
WHERE 1=1
AND CAST(d.dateadded__c AS DATE) >= '2018-01-01'
AND data_size__c < 0
AND c.type__c = 'Transactional'
GROUP BY du.month_and_year, a.Name, m.Name
) AS t2
UNION
SELECT * FROM metrics.tempvault tv
#GROUP BY tv.month_and_year, tv.AccountName, tv.MatterName
UNION
SELECT * FROM(
SELECT
CAST(spc.createddate AS date)
, m.id AS matterid
, m.Name AS MatterName
, a.Name AS AccountName
, CONCAT(u.firstname, ' ', u.lastname) AS owner
, "VAULT" AS Type
, NULL AS mrr
, SUM(spc.datasize__c) AS GbHosted
, a.company_profile__c
, du.week_start_date
, du.week_end_date
, du.month_and_year
FROM metrics.sf_matter__c m
INNER JOIN metrics.sf_partialvault__c spc
ON spc.matter__c = m.id
INNER JOIN metrics.daily_matter_contract_mapping dmcm
ON dmcm.matterid = m.id
AND dmcm.date_value = CAST(spc.createddate AS DATE)
INNER JOIN metrics.sf_contract c
ON c.id = dmcm.contractid
INNER JOIN date_utility du
ON du.date_value = CAST(spc.createddate AS DATE)
INNER JOIN metrics.sf_account a
ON a.id = c.billingaccount__c
LEFT JOIN metrics.sf_sf_user u
ON u.id = m.salesperson__c
LEFT JOIN (
SELECT matterid as MATTERID, month_and_year AS MY
FROM metrics.tempvault
GROUP BY matterid, month_and_year
) AS tv ON tv.MATTERID = m.id AND tv.MY = du.month_and_year
WHERE 1=1
AND CAST(spc.createddate AS date) >= '2018-01-01'
AND c.type__c = 'Transactional'
AND tv.MATTERID IS NULL
AND tv.MY IS NULL
GROUP BY du.month_and_year, a.Name, m.Name
)AS t3;
DROP TABLE IF EXISTS metrics.tempvault;
END $$
DELIMITER ;
Try to avoid '&' in the name of procedure.
Related
I want to add a condition in the procedure, but I can't use (IF(Wh=0,'AND 1=1','AND D.wh_parn = 102'))
This is my procedure:
CREATE DEFINER=`akarremote`#`%` PROCEDURE `laporan_top_brand_penjualan`(IN Wh INT, IN `Datemin` VARCHAR(191), IN `Datemax` VARCHAR(191))
BEGIN
SELECT
A.item_id,
A.item_name AS ITEM,
(
SELECT
SUM( C.jumlah ) AS jumlah
FROM
mob_penjualan B
LEFT JOIN mob_penjualandetail C ON C.noTransaksi = B.noTransaksi
LEFT JOIN krd_wh D ON D.wh_id = B.idWH
WHERE
C.idItemProduk = A.item_id
AND B.TglTransaksi BETWEEN Datemin AND Datemax
(IF(Wh=0,'AND 1=1','AND D.wh_parn = 102'))
GROUP BY
C.idItemProduk
ORDER BY
jumlah DESC
) AS JML,
(
SELECT
SUM( C.qty ) AS qty
FROM
mob_penjualan B
LEFT JOIN mob_penjualandetail C ON C.noTransaksi = B.noTransaksi
LEFT JOIN krd_wh D ON D.wh_id = B.idWH
WHERE
C.idItemProduk = A.item_id
AND B.TglTransaksi BETWEEN Datemin AND Datemax
(IF(Wh=0,'AND 1=1','AND D.wh_parn = 102'))
GROUP BY
C.idItemProduk
ORDER BY
qty DESC
) AS QTY
FROM
krd_item A
ORDER BY
JML DESC, QTY DESC;
END
What's the solution?
WHERE C.idItemProduk = A.item_id
AND B.TglTransaksi BETWEEN Datemin AND Datemax
AND IF(Wh=0, 1, D.wh_parn = 102)
I've a query like this which is working fine but I want to create a pure CI query from this. I'm new in CI so can anyone help me to figure out how this can be done or suggest me a reference website or link from where i can get help. Thanks
SELECT
user_des,
IFNULL((sent),0) as sent,
IFNULL((viewed),0) as viewed,
CONCAT(IFNULL(fname_usr,user_des),' ',lname_usr,user_des) as fullname,
IFNULL(sum(duration),0) as totalviewtime,
IFNULL((totalviews),0) as views,
IFNULL(sum(sharedcount),0) as shares,
IFNULL(sum(shared),0) as shared
FROM dem_senddemo
INNER JOIN
(SELECT * FROM dem_demos WHERE id_dem IN (746,943,748) AND customer_dem = '1') as demosfiltered
ON demo_des = id_dem
LEFT JOIN
(
(
Select senddemo_wis, sum(duration) as duration, max(datereceived_wis) as datereceived_wis
FROM
(
select senddemo_wis, invited_wis, sum(IFNULL(duration_fea,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM (sta_views
LEFT JOIN sta_featureswisitavisits as s ON visit_fvi = id_vis
LEFT JOIN
(
(SELECT shortvideovid_fea as videoid_fea, shortvideoduration_fea as duration_fea
FROM dem_features
where shortvideoduration_fea > 0)
UNION
(SELECT longvideovid_fea as videoid_fea, longvideoduration_fea as duration_fea
FROM dem_features
where longvideoduration_fea > 0)
) as x
ON videoid_fea = showedvideo_fvi)
LEFT JOIN
sta_wistia as w ON invited_fvi = invited_wis AND s.showedvideo_fvi = w.mediaid_wis AND wistia_vis = email_wis
WHERE countvisit_wis <> 'N'
GROUP BY invited_wis
) as durfea
GROUP BY senddemo_wis
) UNION
(
SELECT senddemo_wis,sum(IFNULL(mainvideoduration_dem ,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM sta_wistia as w
INNER JOIN
(select *, customer_dem as cuss FROM dem_demos) as demcorp ON demo_wis = id_dem AND mainmessagingvideovid_dem = w.mediaid_wis
LEFT JOIN sta_views ON email_wis = wistia_vis
WHERE customer_dem = 1 AND senddemo_wis > 0 AND countvisit_wis <> 'N'
GROUP BY senddemo_wis
)
) as c ON id_des = senddemo_wis
LEFT JOIN
(
SELECT user_des as user2_des, count(id_vis) as totalviews
FROM sta_views
LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748)
GROUP BY user_des
) as z ON user_des = user2_des
LEFT JOIN
(
SELECT user_des as user3_des, count(id_des) as sent
FROM dem_senddemo
LEFT JOIN dem_invited ON id_senddemo = id_des
WHERE createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748) AND first_invited = 'YES'
GROUP BY user_des
) as za ON user_des = user3_des
LEFT JOIN
(
SELECT user_des as user4_des, count(id_vis) as viewed
FROM sta_views LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND state = 'VIEWED'
AND first_invited = 'YES' AND demo_des IN (746,943,748)
GROUP BY user_des
) as zb ON user_des = user4_des
LEFT JOIN
(
SELECT id_senddemo as iddemdemos2, count(id_senddemo) as shared, allshares as sharedcount
FROM
(
SELECT id_senddemo, count(id_senddemo) as allshares
FROM dem_invited
LEFT JOIN dem_senddemo on id_senddemo = id_des
WHERE first_invited <> 'YES' AND demo_des IN (746,943,748)
GROUP BY id_senddemo
) as x
GROUP BY id_senddemo
) as zc ON iddemdemos2 = id_des
LEFT JOIN
sec_users ON user_des = id_usr
WHERE customer_dem = 1 AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59'
GROUP BY user_des
Here's the query:
SELECT h.idhour, h.`hour`, outnumber, count(*) as `count`, sum(talktime) as `duration`
FROM (
SELECT
`cdrs`.`dcustomer` AS `dcustomer`,
(CASE
WHEN (`cdrs`.`cnumber` like "02%") THEN '02'
WHEN (`cdrs`.`cnumber` like "05%") THEN '05'
END) AS `outnumber`,
FROM_UNIXTIME(`cdrs`.`start`) AS `start`,
(`cdrs`.`end` - `cdrs`.`start`) AS `duration`,
`cdrs`.`talktime` AS `talktime`
FROM `cdrs`
WHERE `cdrs`.`start` >= #_START and `cdrs`.`start` < #_END
AND `cdrs`.`dtype` = _LATIN1'external'
GROUP BY callid
) cdr
JOIN customers c ON c.id = cdr.dcustomer
LEFT JOIN hub.hours h ON HOUR(cdr.`start`) = h.idhour
WHERE (c.parent = _ID or cdr.dcustomer = _ID or c.parent IN
(SELECT id FROM customers WHERE parent = _ID))
GROUP BY h.idhour, cdr.outnumber
ORDER BY h.idhour;
The above query results skips the hours where there is no data, but I need to show all hours (00:00 to 23:00) with null or 0 values. How can I do this?
SELECT h.idhour
, h.hour
,IFNULL(outnumber,'') AS outnumber
,IFNULL(cdr2.duration,0) AS duration
,IFNULL(output_count,0) AS output_count
FROM hub.hours h
LEFT JOIN (
SELECT HOUR(start) AS start,outnumber, SUM(talktime) as duration ,COUNT(1) AS output_count
FROM
(
SELECT cdrs.dcustomer AS dcustomer
, (CASE WHEN (cdrs.cnumber like "02%") THEN '02' WHEN (cdrs.cnumber like "05%") THEN '05' END) AS outnumber
, FROM_UNIXTIME(cdrs.start) AS start
, (cdrs.end - cdrs.start) AS duration
, cdrs.talktime AS talktime
FROM cdrs cdrs
INNER JOIN customers c ON c.id = cdrs.dcustomer
WHERE cdrs.start >= #_START and cdrs.start < #_END AND cdrs.dtype = _LATIN1'external'
AND
(c.parent = _ID or cdrs.dcustomer = _ID or c.parent IN (SELECT id FROM customers WHERE parent = _ID))
GROUP BY callid
) cdr
GROUP BY HOUR(start),outnumber
) cdr2
ON cdr2.start = h.idhour
ORDER BY h.idhour
You need a table with all hours, nothing else.
Then use LEFT JOIN with the hours table on the "left" and your current query on the "right":
SELECT b.*
FROM hours h
LEFT JOIN ( ... ) b ON b.hr = h.hr
WHERE h.hr BETWEEN ... AND ...
ORDER BY hr;
Any missing hours will be NULLs in b.*.
really do not know if you can, but i I need the DATE VENC to be equal to '2013-02-02'.
the values of the column date_pay:
1-2013-01-01
2-2013-02-02
3-0000-00-00
4-0000-00-00
this is my query:
SELECT s.id,
s.name,
s.nro_s,
ts.cat,
SUM( ts.pryce ) AS deuda,
SUM( ts.pryce ) DIV ts.pryce AS c_p,
date_venc = (select max(date_pay) from c ) // the date in question
FROM s
INNER JOIN c
INNER JOIN ts
WHERE s.id = '123'
AND c.id = '123'
AND c.date_pay = '0000-00-00'
AND s.ts = ts.id_ts
Sorry for my english, is very basic.
Greetings.
Assuming date_venc is DATE a possible solution
select *
from s
where s.date_venc=
(select max(cast(SUBSTRING_INDEX(date_pay,'-',-3)as DATE))from c);
also check out sqlfiddle
http://sqlfiddle.com/#!2/64197/1
and your query should probably be modified to,
SELECT s.id,
s.name,
s.nro_s,
ts.cat,
SUM( ts.pryce ) AS deuda,
SUM( ts.pryce ) DIV ts.pryce AS c_p,
date_venc
FROM s
INNER JOIN c
INNER JOIN ts
WHERE s.id = '123'
AND c.id = '123'
AND c.date_pay = '0000-00-00'
AND s.ts = ts.id_ts
AND date_venc = (select max(cast(SUBSTRING_INDEX(date_pay,'-',-3)as DATE)) from c ) // the date in question
We have a long sql procedure that takes limit & amount(limitCount) parameters. So, we are using concat statement for joining multiple queries. Calling this procedure gives err.no 1064 when we try to run it.
EDIT : Based on the comment, I'm adding the whole code.
CREATE PROCEDURE getProfileTasks (IN p_id1 INT, IN p_id2 INT , IN limitStart INT, IN limitCount INT)
BEGIN
SET #SQL = CONCAT('
SELECT P.access_type INTO #privacy FROM Profile P WHERE P.profile_id = ' , p_id2 , ';
IF( ' , p_id1, ' = ' ,p_id2 , ')
THEN
SELECT T.task_id, T.name, D.add_time, D.location, DATE_FORMAT(D.date1, "%d/%m/%y") as `date1`, D.time3, D.state, TIME_FORMAT(D.time1, "%H:%i")as `time1`, D.does_id, IFNULL(L.Like, 0) AS `LikeCount` , IFNULL(C.CommentCount,0) AS `CommentCount` FROM Task T
INNER JOIN Does D on D.task_id = T.task_id
INNER JOIN Profile P on P.profile_id = D.profile_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(L.profile_id) as `Like` FROM `Likes` L
INNER JOIN Does D on D.does_id = L.does_id
INNER JOIN Profile P on P.profile_id = D.profile_id
WHERE P.profile_id = ' , p_id2 , '
GROUP BY does_id) L on L.does_id = D.does_id
LEFT OUTER JOIN( SELECT D.does_id, COUNT(C.content) AS `CommentCount` FROM Comment C
INNER JOIN Does D on D.does_id = C.does_id
GROUP BY (D.does_id)) C ON C.does_id = D.does_id
WHERE P.profile_id= ' , p_id2, ' ORDER BY D.add_time DESC LIMIT ' , limitStart , ', ' , limitCount, ';
ELSE
IF (#privacy = 0)
THEN
SELECT T.task_id, T.name, D.add_time, D.location, DATE_FORMAT(D.date1, "%d/%m/%y") as `date1`, D.time3, D.state, TIME_FORMAT(D.time1, "%H:%i")as `time1`, D.does_id, IFNULL(L.Like,0) AS `LikeCount`, IFNULL(C.CommentCount,0) AS `CommentCount` FROM Task T
INNER JOIN Does D on D.task_id = T.task_id
INNER JOIN Profile P on P.profile_id = D.profile_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(L.profile_id) as `Like` FROM `Likes` L
INNER JOIN Does D on D.does_id = L.does_id
INNER JOIN Profile P on P.profile_id = D.profile_id
WHERE P.profile_id = ' , p_id2 , '
GROUP BY does_id) L on L.does_id = D.does_id
LEFT OUTER JOIN( SELECT D.does_id, COUNT(C.content) AS `CommentCount` FROM Comment C
INNER JOIN Does D on D.does_id = C.does_id
GROUP BY (D.does_id) )C ON C.does_id = D.does_id
WHERE P.profile_id= ' ,p_id2, ' ORDER BY D.add_time DESC LIMIT ' , limitStart , ', ' , limitCount, ';
ELSE
IF EXISTS ( SELECT * FROM Follows F
INNER JOIN Profile P on F.follower_id = P.profile_id
INNER JOIN Profile P2 on F.following_id = P2.profile_id
WHERE (P.profile_id = ' , p_id1, ' AND P2.profile_id = ' , p_id2 , '))
THEN
SELECT T.task_id, T.name, D.add_time, D.location, DATE_FORMAT(D.date1, "%d/%m/%y") as `date1`, D.time3, D.state, TIME_FORMAT(D.time1, "%H:%i")as `time1`, D.does_id, IFNULL(L.Like,0) AS `LikeCount`, IFNULL(C.CommentCount,0) AS `CommentCount` FROM Task T
INNER JOIN Does D on D.task_id = T.task_id
INNER JOIN Profile P on P.profile_id = D.profile_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(L.profile_id) as `Like` FROM `Likes` L
INNER JOIN Does D on D.does_id = L.does_id
INNER JOIN Profile P on P.profile_id = D.profile_id
WHERE P.profile_id = ' , p_id2 , '
GROUP BY does_id) L on L.does_id = D.does_id
LEFT OUTER JOIN( SELECT D.does_id, COUNT(C.content) AS `CommentCount` FROM Comment C
INNER JOIN Does D on D.does_id = C.does_id
GROUP BY (D.does_id) )C ON C.does_id = D.does_id
WHERE P.profile_id= ' , p_id2 , ' ORDER BY D.add_time DESC LIMIT ' , limitStart , ', ' , limitCount, ';
END IF;
END IF;
END IF; '
);
PREPARE query FROM #SQL;
EXECUTE query;
DEALLOCATE PREPARE query;
END
Anyone has any idea about why we are getting this error ?
ERROR : #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 'IF( 18 = 18) THEN SELECT T.task_id, T.name, D.add_time, D.location, DATE_' at line 2
P.S : DELIMITER is set to //
Since the way the query in the stored procedure was created isn't really server-friendly (nor friendly to other developers) I have reformatted the stored procedure. Then I also noted that it's actually the same query that's executed in three cases, so there is a lot of duplication.
CREATE PROCEDURE getProfileTasks (IN p_id1 INT, IN p_id2 INT , IN limitStart INT, IN limitCount INT)
BEGIN
DECLARE privacy INT;
DECLARE is_following INT;
SELECT P.access_type INTO privacy FROM Profile P WHERE P.profile_id = p_id2;
SELECT COUNT(*) INTO is_following
FROM Follows F
INNER JOIN Profile P on F.follower_id = P.profile_id
INNER JOIN Profile P2 on F.following_id = P2.profile_id
WHERE (P.profile_id = p_id1 AND P2.profile_id = p_id2)
LIMIT 1;
IF ( (p_id1 = p_id2) OR (privacy = 0) OR (is_following > 0) )
THEN
-- Using CONCAT and prepared statement because LIMIT won't work otherwise
#SQL = CONCAT('
SELECT
T.task_id
, T.name
, D.add_time
, D.location
, DATE_FORMAT(D.date1, "%d/%m/%y") AS `date1`
, D.time3
, D.state
, TIME_FORMAT(D.time1, "%H:%i") AS `time1`
, D.does_id
, IFNULL(L.`Like`, 0) AS `LikeCount`
, IFNULL(C.CommentCount,0) AS `CommentCount`
FROM Task T
INNER JOIN Does D on D.task_id = T.task_id
INNER JOIN Profile P on P.profile_id = D.profile_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(L.profile_id) AS `Like`
FROM `Likes` L
INNER JOIN Does D on D.does_id = L.does_id
INNER JOIN Profile P on P.profile_id = D.profile_id
WHERE P.profile_id = #p_id2
GROUP BY does_id
) L on L.does_id = D.does_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(C.content) AS `CommentCount`
FROM Comment C
INNER JOIN Does D on D.does_id = C.does_id
GROUP BY (D.does_id)
) C ON C.does_id = D.does_id
WHERE P.profile_id= #p_id2
ORDER BY D.add_time DESC
LIMIT ', limitStart, ', ' , limitCount, ';');
SET #p_id2 = p_id2;
PREPARE query FROM #SQL;
EXECUTE query USING #p_id2;
DEALLOCATE PREPARE query;
END IF;
END
And I also added backticks around Like in IFNULL(L.`Like`, 0) AS `LikeCount`, because that's a potential problem as pointed out in the comments of the question.