SQL sum inside the value - mysql

i have this kind of result but i want to sum up some of the value inside the value. Sorry for my english. Refer image for better understanding.
Expected Result:
My sql query :
SELECT
itmnocate.Source as Local,
itmnocate.GradeCategory,
sum(sales_data.QUANTITY/1000) AS UnitMT
FROM
sales_data
INNER JOIN itmnocate
ON sales_data.ITEM = itmnocate.ItemNumber
WHERE
sales_data.unit = 'KG'
AND
sales_data.CUSTOMERACCOUNT not in ('CT1008','CT1009')
AND itmnocate.Source in ('local','by product')
GROUP BY itmnocate.GradeCategory

WITH your_query
AS (SELECT 'BY PRODUCT' AS local,
'BY PRODUCT' AS "GradeCategory 1",
3380.59 AS unitmt
UNION ALL
SELECT 'LOCAL' AS local,
'LOCAL OTHERS' AS "GradeCategory 1",
2754.19 AS unitmt
UNION ALL
SELECT 'LOCAL' AS local,
'SUPER 15' AS "GradeCategory 1",
19598.17 AS unitmt
UNION ALL
SELECT 'LOCAL' AS local,
'TENDER/RAMPASAN' AS "GradeCategory 1",
53.65 AS unitmt
)
select MAX(local), sub_category,SUM(unitmt) from (SELECT local,
"GradeCategory 1",
unitmt,
CASE "GradeCategory 1" WHEN 'SUPER 15' THEN 'SUPER 15' ELSE 'LOCAL
OTHERS' END
AS sub_category
FROM your_query) processd_data group by sub_category

You can do what you want using a CASE expression to calculate the new category values:
SELECT 'LOCAL' as Local,
(CASE WHEN i.GradeCategory = 'SUPER 15' THEN 'SUPER 15'
ELSE 'OTHERS'
END) as Cat,
SUM(s.QUANTITY/1000) AS UnitMT
FROM sales_data s INNER JOIN
itmnocate i
ON s.ITEM = i.ItemNumber
WHERE s.unit = 'KG' AND
s.CUSTOMERACCOUNT NOT IN ('CT1008', 'CT1009') AND
i.Source in ('local', 'by product')
GROUP BY cat;

Related

SQL - how to display products' options, whether or not they have them?

Assume a products can have 0 or more options -- implemented with the following tables:
products
- id
- name
options
- id
- name
product_options
- id
- product_id
- option_id
Further assume the following products have the following options:
Product 1 = Option 1, Option 2, Option 3
Product 2 = Option 2, Option 3, Option 4
Product 3 = Option 3, Option 4, Option 5
How can I query this so that I get results like this:
Product 1, Option 1, Option 2, Option 3, NULL, NULL
Product 2, NULL, Option 2, Option 3, Option 4, NULL
Product 3, NULL, NULL, Option 3, Option 4, Option 5
My options are actually a nested tree. And they have a foreign key to a categories table (also a nested tree). Ultimately, I need to be able to do this query and group the results by category. However, I probably should understand first how to solve this simpler version of my problem.
UPDATE 1: I do not know in advance what the options might be. Also, there is no limit to the number of options a product may have.
If you have an unknown number of options you could use a stored procedure to dynamically create a query that can be used to pivot your table. Something like this:
CREATE PROCEDURE display_options()
BEGIN
SET #query = 'SELECT p.id, ';
SET #query = CONCAT(#query, (SELECT GROUP_CONCAT(CONCAT('MAX(CASE WHEN o.name = ''', name, ''' THEN o.name END) AS `', name, '`')) FROM options ORDER BY id));
SET #query = CONCAT_WS(' ', #query,
'FROM products p',
'JOIN product_options po ON po.product_id = p.id',
'JOIN options o ON o.id = po.option_id',
'GROUP BY p.id');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
This procedure will produce a query like this (essentially the same as in #GordonLinoff's answer for the sample data in your question):
SELECT p.name,
MAX(CASE WHEN o.name = 'Option 1' THEN o.name END) AS `Option 1`,
MAX(CASE WHEN o.name = 'Option 2' THEN o.name END) AS `Option 2`,
MAX(CASE WHEN o.name = 'Option 3' THEN o.name END) AS `Option 3`,
MAX(CASE WHEN o.name = 'Option 4' THEN o.name END) AS `Option 4`,
MAX(CASE WHEN o.name = 'Option 5' THEN o.name END) AS `Option 5`
FROM products p
JOIN product_options po ON po.product_id = p.id
JOIN options o ON o.id = po.option_id
GROUP BY p.name
which can then be prepared and executed to give results like this:
name Option 1 Option 2 Option 3 Option 4 Option 5
Product 1 Option 1 Option 2 Option 3
Product 2 Option 2 Option 3 Option 4
Product 3 Option 3 Option 4 Option 5
demo on dbfiddle
After so many of trials I found below query that will return exact result as you want even if options and product may increases.
SELECT CONCAT(P.productName, ",", GROUP_CONCAT(COALESCE(IF(P.optionId IN (product_options.option_id),P.optionName,NULL),"NULL") order by P.optionId)) AS result
FROM product_options
RIGHT JOIN (SELECT products.id as productId, products.name as productName,options.id as optionId, options.name AS optionName from products,options)
as P On P.productId = product_options.product_id AND P.optionId = product_options.option_id
GROUP BY P.productId
You can use conditional aggregation, if I understand correctly:
select po.product_id,
max(case when o.name = 'Option 1' then o.name end) as option_1,
max(case when o.name = 'Option 2' then o.name end) as option_2,
max(case when o.name = 'Option 3' then o.name end) as option_3,
max(case when o.name = 'Option 4' then o.name end) as option_4,
max(case when o.name = 'Option 5' then o.name end) as option_5
from product_options po join
options o
on po.option_id = o.id
group by po.product_id
Do you really need to structure the display at the DB level?
Honestly, if it's at all feasible, I'd save myself the headace and run:
SELECT p.*, o.*
FROM product_options po
JOIN products p
ON p.id = po.product_id
JOIN options o
ON o.id = po.option_id
WHERE ...
ORDER BY ...
You can then sort out your desired structure at the application level as you iterate through the result set
Product options are important in ecommerce, usually more complicated than above given question.
For example in any standard application say 'Opencart' we have following 6 tables for options
Table occw_option - columns: option_id, type
Table occw_option_description -columns: option_id, language_id, name
Table occw_option_value -columns: option_id, option_value_id, image
Table occw_option_value_description - columns: option_value_id, language_id,option_id, name
Table occw_product_option -colums: product_option_id, product_id, option_id, value
Table occw_product_option_value - columns: product_option_value_id,product_option_id, product_id, option_id, option_value_id
quantity, price price_prefix, points, points_prefix, weight, weight_prefix
Easiest and best thing to do is to use left joins
like for example say your 'product_id' is '59'
SELECT product_option_value_id,product_option_id, product_id, quantity, price, price_prefix, points, points_prefix, weight, weight_prefix, occw_option_description.name, occw_option_description.language_id, occw_option.type,occw_option_value.image, occw_option_value_description.name
FROM `occw_product_option_value`
left join `occw_option` on occw_product_option_value.option_id = occw_option.option_id
left join `occw_option_description` on occw_product_option_value.option_id = occw_option_description.option_id
left join `occw_option_value` on occw_product_option_value.option_value_id = occw_option_value.option_value_id
left join `occw_option_value_description` on occw_option_value.option_value_id = occw_option_value_description.option_value_id
WHERE occw_product_option_value.product_id = 59
You will get nicely all info you require for your front end like below
product_option_value_id, product_option_id, product_id, quantity,price, price_prefix, points, points_prefix, weight, weight_prefix, name, language_id, type, image, name
84 250 59 111 0.00 + 0 + 0.00 + Brand 1 radio XLL
83 250 59 122 0.00 + 0 + 0.00 + Brand 1 radio XL
82 250 59 45 0.00 + 0 + 0.00 + Brand 1 radio M
81 250 59 34 0.00 + 0 + 0.00 + Brand 1 radio L
These above are say for 'product' shirt sizes options XLL, XL, M, L which a customer can choose by 'radio' at the front end

MySQL - Slow Query when adding multiple derived tables - Optimization

For my query, the two derived tables at the bottom are causing a crazy slow up for this query. The query, as is, takes about 45-55 seconds to execute.. NOW, when i remove just one of those derived tables (it does not matter which one) the query goes down to 0.1 - 0.3 seconds. My questions; Is there an issue with having multiple derived tables? Is there a better way to execute this? My indexes all seem to be correct, I will also include the explain from this query.
select t.name as team, u.name as "REP NAME",
count(distinct activity.id) as "TOTAL VISITS",
count(distinct activity.account_id) as "UNIQUE VISITS",
count(distinct placement.id) as "COMMITMENTS ADDED",
CASE WHEN
count(distinct activity.account_id) = 0 THEN (count(distinct
placement.id) / 1)
else (cast(count(distinct placement.id) as decimal(10,2)) /
cast(count(distinct activity.account_id) as decimal(10,2)))
end as "UNIQUE VISIT TO COMMITMENT %",
case when o.mode='basic' then count(distinct placement.id) else
count(distinct(case when placement.commitmentstatus='fullfilled'
then placement.id else 0 end))
end as "COMMITMENTS FULFILLED",
case when o.mode='basic' then 1 else
(CASE WHEN
count(distinct placement.id) = 0 THEN (count(distinct(case when
placement.commitmentstatus='fullfilled' then placement.id else 0
end)) / 1)
else (cast(count(distinct(case when
placement.commitmentstatus='fullfilled' then placement.id else 0
end)) as decimal(10,2)) / cast(count(distinct placement.id) as
decimal(10,2)))
end) end as "COMMITMENT TO FULFILLMENT %"
from lpmysqldb.users u
left join lpmysqldb.teams t on t.team_id=u.team_id
left join lpmysqldb.organizations o on o.id=t.org_id
left join (select * from lpmysqldb.activity where
org_id='555b918ae4b07b6ac5050852' and completed_at>='2018-05-01' and
completed_at<='2018-06-01' and tag='visit' and accountname is not
null and (status='active' or status='true' or status='1')) as
activity on activity.user_id=u.id
left join (select * from lpmysqldb.placements where
orgid='555b918ae4b07b6ac5050852' and placementdate>='2018-05-01' and
placementdate<='2018-06-01' and (status IN ('1','active','true') or
status is null)) as placement on placement.userid=u.id
where u.org_id='555b918ae4b07b6ac5050852'
and (u.status='active' or u.status='true' or u.status='1')
and istestuser!='1'
group by u.org_id, t.name, u.id, u.name, o.mode
order by count(distinct activity.id) desc
Thank you for assistance!
I have edited below with changing the two bottom joins from joining on subqueries to joining on the table directly. Still yielding the same result.
This is a SLIGHTLY restructured query of your same. Might be simplified as the last two subqueries are all pre-aggregated for your respective counts and count distincts so you can use those column names directly instead of showing all the count( distinct ) embedded throughout the query.
I also tried to simplify the division by multiplying a given count by 1.00 to force decimal-based precision as result.
select
t.name as team,
u.name as "REP NAME",
Activity.DistIdCnt as "TOTAL VISITS",
Activity.UniqAccountCnt as "UNIQUE VISITS",
Placement.DistIdCnt as "COMMITMENTS ADDED",
Placement.DistIdCnt /
CASE WHEN Activity.UniqAccountCnt = 0
THEN 1.00
ELSE Activity.UniqAccountCnt * 1.00
end as "UNIQUE VISIT TO COMMITMENT %",
case when o.mode = 'basic'
then Placement.DistIdCnt
else Placement.DistFulfillCnt
end as "COMMITMENTS FULFILLED",
case when o.mode = 'basic'
then 1
else ( Placement.DistFulfillCnt /
CASE when Placement.DistIdCnt = 0
then 1.00
ELSE Placement.DistIdCnt * 1.00
END TRANSACTION )
END as "COMMITMENT TO FULFILLMENT %"
from
lpmysqldb.users u
left join lpmysqldb.teams t
on u.team_id = t.team_id
left join lpmysqldb.organizations o
on t.org_id = o.id
left join
( select
user_id,
count(*) as AllRecs,
count( distinct id ) DistIdCnt,
count( distinct account_id) as UniqAccountCnt
from
lpmysqldb.activity
where
org_id = '555b918ae4b07b6ac5050852'
and completed_at>='2018-05-01'
and completed_at<='2018-06-01'
and tag='visit'
and accountname is not null
and status IN ( '1', 'active', 'true')
group by
user_id ) activity
on u.id = activity.user_id
left join
( select
userid,
count(*) AllRecs,
count(distinct id) as DistIdCnt,
count(distinct( case when commitmentstatus = 'fullfilled'
then id
else 0 end )) DistFulfillCnt
from
lpmysqldb.placements
where
orgid = '555b918ae4b07b6ac5050852'
and placementdate >= '2018-05-01'
and placementdate <= '2018-06-01'
and ( status is null OR status IN ('1','active','true')
group by
userid ) as placement
on u.id = placement.userid
where
u.org_id = '555b918ae4b07b6ac5050852'
and u.status IN ( 'active', 'true', '1')
and istestuser != '1'
group by
u.org_id,
t.name,
u.id,
u.name,
o.mode
order by
activity.DistIdCnt desc
FINALLY, your inner queries are querying for ALL users. If you have a large count of users that are NOT active, you MIGHT exclude those users from each inner query by adding those join/criteria there too such as...
( ...
from
lpmysqldb.placements
JOIN lpmysqldb.users u2
on placements.userid = u2.id
and u2.status IN ( 'active', 'true', '1')
and u2.istestuser != '1'
where … ) as placement

Speed up query on mysql view

I have a very long query with multiple sub queries and this query can take up to 30 sec to complete.
Is there a better way to construct the query?
select FROM_UNIXTIME(peg_remedy_incident.PEG_OutageStartTime,"%m-%d-%Y") AS 'Incident Date', PEG_Category AS Category, (select GROUP_CONCAT(DISTINCT peg_frm_inc_incidentXnetworkRe.Market ORDER BY Market) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types IN (1,2) GROUP BY peg_frm_inc_incidentXnetworkRe.IncidentVisibleID, peg_frm_inc_incidentXnetworkRe.CustomerCode) AS Market,peg_frm_inc_incidentXcustomerR.CustomerName AS 'Customer' , peg_frm_inc_incidentXcustomerR.CustomerTicket AS 'Customer TT#', peg_remedy_incident.PEG_NetworkProvider AS 'Network Provider', peg_remedy_incident.PEG_ProviderTicketNumber AS 'Network Provider TT#',
peg_remedy_incident.PEG_ThirdPartyVendor AS '3rd Party Support Vendor', peg_remedy_incident.PEG_ThirdPartyVendor_TT_ AS '3rd Party TT#','' AS 'Maintenance Window Outage? (Y/N)', TRIM(LEADING '0' FROM peg_frm_inc_incidentXcustomerR.IncidentID) AS 'TT#',FROM_UNIXTIME(peg_remedy_incident.PEG_OutageStartTime,"%m-%d-%Y %r") AS `Event Start Time`, FROM_UNIXTIME(peg_remedy_incident.PEG_OutageStopTime,"%m-%d-%Y %r") AS `Event Stop Time`,
'=(M{ROWID}-L{ROWID})*1440' AS 'Total Minutes', '=(M{ROWID}-L{ROWID})' AS 'Total Hours/Minutes','=(M{ROWID}-L{ROWID})*R{ROWID}*1440' AS ' Total Outage Minutes','=(M{ROWID}-L{ROWID})*R{ROWID}' AS ' Total Outage Hours/Minutes',
(select count(DISTINCT(Site_ID)) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=1) AS 'Outage Sites Impacted',
(select count(DISTINCT(Circuit_ID)) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND
peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=1) AS 'Outage Circuits Impacted',
(select count(DISTINCT(Site_ID)) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=2) AS 'Simplex Sites Impacted',
(select count(DISTINCT(Circuit_ID)) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND
peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=2) AS 'Simplex Circuits Impacted',
peg_remedy_incident.PEG_TechName AS 'Technician Dispatch', '' AS 'Summary & Ticket Notes', peg_remedy_incident.PEG_NetworkAvailabilityAffecti ,
CAST( CASE
WHEN peg_remedy_incident.PEG_Ring_Lateral = 'Lateral'
THEN 'Yes'
WHEN peg_remedy_incident.PEG_Ring_Lateral = 'Ring'
THEN 'No'
ELSE ''
END AS char(3)) as 'Lateral (Y/N)' ,
'' AS 'Backhaul/Non-Backhaul?', PEG_Resolution_Details AS 'Resolution Details',
PEG_CauseCode1 AS 'Cause Code 1', PEG_CauseCode2 AS 'Cause Code 2',
PEG_CauseCode3 AS 'Cause Code 3', PEG_CauseCode4 AS 'Cause Code 4', Root_Cause_Analysis AS 'Root Cause Analysis','' AS 'Notes',
(select GROUP_CONCAT(DISTINCT peg_frm_inc_incidentXwork_log.Notes ORDER BY Request_ID) AS 'Work Log1' from peg_frm_inc_incidentXwork_log WHERE peg_frm_inc_incidentXwork_log.Incident_ID=peg_frm_inc_incidentXcustomerR.IncidentID GROUP BY peg_frm_inc_incidentXwork_log.Incident_ID) as 'Work Log',
(select GROUP_CONCAT(DISTINCT peg_frm_inc_incidentXnetworkRe.Site_ID ORDER BY Site_ID) AS 'OutageSite List1' from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=1 GROUP BY peg_frm_inc_incidentXnetworkRe.IncidentVisibleID, peg_frm_inc_incidentXnetworkRe.CustomerCode) as 'Outage Site List'
FROM peg_frm_inc_incidentXcustomerR LEFT JOIN peg_frm_inc_incidentXnetworkRe ON peg_frm_inc_incidentXcustomerR.IncidentID=peg_frm_inc_incidentXnetworkRe.IncidentVisibleID
LEFT JOIN peg_remedy_incident ON peg_frm_inc_incidentXcustomerR.IncidentID=peg_remedy_incident.PEG_VisibleTicketID WHERE peg_frm_inc_incidentXnetworkRe.RadioButton_Types IN(1 ,2) AND peg_remedy_incident.PEG_VisibleTicketID>=000000000034000 GROUP BY peg_frm_inc_incidentXnetworkRe.IncidentVisibleID,peg_frm_inc_incidentXcustomerR.CustomerCode

How to create query that groups Years

I have this query:
SELECT Count(tblpeople.PersonID) AS Total,
YEAR(CURRENT_TIMESTAMP) - YEAR(tblmembership.MemberSince) AS YearsMember
FROM tblmembership
INNER JOIN tblpeople ON tblmembership.PersonID = tblpeople.PersonID
WHERE
tblpeople.MemSTATUS = 'Back from VPM'
OR tblpeople.MemSTATUS = 'Sent To VPM 2'
OR tblpeople.MemSTATUS = 'Sent To VPM 1'
OR tblpeople.MemSTATUS = 'Back from VPM'
OR tblpeople.MemSTATUS = 'Renewal'
OR tblpeople.MemSTATUS = 'Active Member'
GROUP BY YearsMember
HAVING YearsMember
ORDER BY YearsMember
It gets counts on years people have been a member.
What has me stumped is the boss wants it broken down by:
0 - 2,
3 -5,
6 - 10,
11 - 20,
>20
So counts for each. I can not figure out how to do this, though I'm sure there's a way!
I can just bring this into excel and manually group the counts, but wanted it done all in one shot.
Thanks
Use a case when expression: http://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm
case when years between 0 and 2 then ... else ...
Use IN instead of all those OR statements. Then use a CASE statement to group your results:
SELECT Count(tblpeople.PersonID) AS Total,
CASE
WHEN YEAR(CURRENT_TIMESTAMP) - YEAR(tblmembership.MemberSince) BETWEEN 0 AND 2
THEN '0-2'
WHEN YEAR(CURRENT_TIMESTAMP) - YEAR(tblmembership.MemberSince) BETWEEN 3 AND 5
THEN '3-5',
...
WHEN YEAR(CURRENT_TIMESTAMP) - YEAR(tblmembership.MemberSince) > 20
THEN '>20'
END AS YearsMember
FROM tblmembership
INNER JOIN tblpeople ON tblmembership.PersonID = tblpeople.PersonID
WHERE tblpeople.MemSTATUS IN ( 'Back from VPM', 'Sent To VPM 2', 'Sent To VPM 1',
'Back from VPM', 'Renewal', 'Active Member')
GROUP BY YearsMember
ORDER BY YearsMember
SQL Fiddle Demo

How to write union query in doctrine?

I want to use union in doctrine, i searched a lot but didn't get any success, this is my union query in sql, how to convert this query in doctrine?
select * from (select orderid,tutorialId,points,allow_multiple,question,answer1,image1,correct1,answer2,image2,correct2,answer3,image3,correct3,answer4,image4,correct4,answer5,image5,correct5,'1' as istest,'' as content,'' as media,'' as media_type_id from tutorial_test
union
select orderid,tutorialId,'0' as istest,content,media,media_type_id,'' as points,'' as allow_multiple,'' as question,'' as answer1,'' as image1,'' as correct1,'' as answer2,'' as image2,'' as correct2,'' as answer3,'' as image3,'' as correct3,'' as answer4,'' as image4,'' as correct4,'' as answer5,'' as image5,'' as correct5 from tutorial_elements) a where a. tutorialId = 1 order by orderid asc
AND this one is my doctrine query
$query = "SELECT * FROM(SELECT
tt.tutorialid
FROM
TutorialTest tt
UNION
SELECT te.tutorialid) tte
WHERE tte.tutorialid = 1
";
$qb = $this->Doctrine->createQuery($query);
$tutorial_test = $qb->getResult();
i researched alot but didn't get any success, if any one can help, million of thanks in advance fot that.
Well i have found a solution
We can use this query with RSM as following
"Usman is basically table name and class"
$rsm = new ResultSetMapping();
$rsm->addEntityResult('Usmans', 'u');
$rsm->addFieldResult('u', 'orderid', 'orderid');
$rsm->addFieldResult('u', 'tutorialId', 'tutorialid');
$rsm->addFieldResult('u', 'points', 'points');
$query = $this->Doctrine->createNativeQuery('SELECT * FROM usman', $rsm);
$tutorial_tests = $query->getResult();
AND we can use without ORM as
$testQuery = "
select * from (
select orderid,
tutorialId,
points,
allow_multiple,
question,
answer1,
image1,
correct1,
answer2,
image2,
correct2,
answer3,
image3,
correct3,
answer4,
image4,
correct4,
answer5,
image5,
correct5,
'1' as istest,
'' as content,
'' as media,
'' as media_type_id
from tutorial_test
union
select orderid,
tutorialId,
'0' as istest,
content,
media,
media_type_id,
'' as points,
'' as allow_multiple,
'' as question,
'' as answer1,
'' as image1,
'' as correct1,
'' as answer2,
'' as image2,
'' as correct2,
'' as answer3,
'' as image3,
'' as correct3,
'' as answer4,
'' as image4,
'' as correct4,
'' as answer5,
'' as image5,
'' as correct5
from tutorial_elements
) a
where a. tutorialId = $tutorial_id
order by orderid asc
";
$resultSets = $this->Doctrine->getConnection()->fetchAll($testQuery);
For Union Query has some Rules
(1) All SELECT Statements has same data type and same no of columns
In your select query has different datatype, no of columns are not match.
So you found Proble.
here is solution
select orderid, tutorialId, points, allow_multiple, question, answer1, image1, correct1, answer2, image2, correct2, answer3, image3, correct3, answer4, image4, correct4, answer5, image5, correct5,'1' as istest,'' as content,'' as media,'' as media_type_id
from tutorial_test
union
select orderid, tutorialId,'0' as istest, content, media, media_type_id,'' as points,'' as allow_multiple,'' as question,'' as answer1,'' as image1,'' as correct1,'' as answer2,'' as image2,'' as correct2,'' as answer3,'' as image3,'' as correct3,'' as answer4,'' as image4,'' as correct4,'' as answer5,'' as image5,'' as correct5, '1' as istest,'' as content,'' as media,'' as media_type_id
from tutorial_elements
where a. tutorialId = 1
order by orderid asc