I have query:
SELECT p.`obj_id` ,
p.`alt_name` ,
o.`name`,
p.`id`,
oc.`text_val`,
oc.`float_val`
FROM `cms3_hierarchy` p
LEFT JOIN `cms3_objects` o
ON p.`obj_id` = o.`id`
LEFT JOIN `cms3_object_content` oc
ON p.`obj_id` = oc.`obj_id`
WHERE (oc.`field_id` = 221 OR oc.`field_id` = 248 )
AND (p.`rel`=903687) LIMIT 0,50
But answer like this:
obj_id name id 221 248
1 first 2 null
1 first null 3
Well, i have one obj_id with different values.
But for me this is look like this:
obj_id name id 221 248
1 first 2 3
How to do this?
Conditions on the right table of a LEFT JOIN should be specified only inside the ON clause, when they are in the WHERE clause, the join automatically turns into an INNER JOIN because of NULL comparison.
Also, use IN() to compare the same column to multiple values instead of OR . I also used MAX() to group the rows into one row, so :
SELECT p.obj_id , p.alt_name , o.name,p.id,
MAX(CASE WHEN oc.field_id = 221 THEN oc.text_val END) as col_221,
MAX(CASE WHEN oc.field_id = 1123 THEN oc.text_val END) as col_1123,
MAX(oc.float_val)
FROM cms3_hierarchy p
LEFT JOIN cms3_objects o
ON p.obj_id = o.id
LEFT JOIN cms3_object_content oc
ON p.obj_id = oc.obj_id and oc.field_id in(221,248,1123)
WHERE p.rel=903687
LIMIT 0,50
GROUP BY p.obj_id , p.alt_name , o.name,p.id
Related
I have 3 different table named job_party, job_party_details and job_party_delv. In which job_party is the main table and others are detailed tables. I am trying to gather data date by date from all these tables. I wrote the following query and getting perfect data but the problem is data of job_party_details comes first and job_party_delv comes latter. I want all data at once as per the date.
SELECT job_party.on_date
, SUM(job_party_details.qty) as detail_qty
, NULL as delv
FROM job_party_details d
JOIN job_party p
on d.jp_id = p.id
where p.party_id = 9
and d.i_id = 1
GROUP
BY p.on_date
UNION
SELECT p.on_date
, NULL as detail_qty
, SUM(d.d_qty) as delv
FROM job_party_delv d
JOIN job_party p
on d.jp_id = p.id
where p.party_id = 9
and d.i_id = 1
GROUP
BY p.on_date
Replace NULL with 0 then make the query as a sub-query and perform another SUM on the outer query GROUP BY on_date like this:
SELECT on_date,SUM(detail_qty) as detail_qty, SUM(delv) as delv
FROM
(SELECT job_party.on_date
, SUM(job_party_details.qty) as detail_qty
, 0 as delv
FROM job_party_details d
JOIN job_party p
on d.jp_id = p.id
where p.party_id = 9
and d.i_id = 1
GROUP
BY p.on_date
UNION
SELECT p.on_date
, 0 as detail_qty
, SUM(d.d_qty) as delv
FROM job_party_delv d
JOIN job_party p
on d.jp_id = p.id
where p.party_id = 9
and d.i_id = 1
GROUP
BY p.on_date) A GROUP BY on_date;
I am writing a mysql select sub query, that is working fine, it returns 2 column is there way to select only 1 column.
My query is
SELECT sum(fl.qunt) as qunt,(
SELECT GROUP_CONCAT( xp.id
SEPARATOR ',' )
FROM prdt AS xp
LEFT JOIN prdt_fac AS pf ON pf.fk_product_children = xp.rowid
WHERE pf.prdt_fat = p.id
AND pf.prdt_ch = 6953
GROUP BY pf.prdt_fat
LIMIT 0 , 1
) AS prdt_chd
FROM fac_log AS fl
LEFT JOIN fac AS f ON fl.fac = f.id
LEFT JOIN prdt AS p ON f.prdt = p.id
GROUP BY prod_child
ORDER BY fl.tms DESC
LIMIT 0 , 1
This return two column qunt and prdt_ch.
But i want only column in result qunt.
Is there any way, becuase inner select query is must be used to get correct result.
Main purpose of this query is that i have to use this query as sub query in an other query, in this condition it throws error "operand should contain 1 column"
Thanks in advance
just select qunt using your select query as a table (t)
select qunt from (
SELECT sum(fl.qunt) as qunt,(
SELECT GROUP_CONCAT( xp.id
SEPARATOR ',' )
FROM prdt AS xp
LEFT JOIN prdt_fac AS pf ON pf.fk_product_children = xp.rowid
WHERE pf.prdt_fat = p.id
AND pf.prdt_ch = 6953
GROUP BY pf.prdt_fat
LIMIT 0 , 1
) AS prdt_chd
FROM fac_log AS fl
LEFT JOIN fac AS f ON fl.fac = f.id
LEFT JOIN prdt AS p ON f.prdt = p.id
GROUP BY prod_child
ORDER BY fl.tms DESC
LIMIT 0 , 1 ) t
I wrote and would like to combine these 2 sql, one is based on results of another. I checked this post, but looks like its not results based. How could I achieve it ?
First sql:
SELECT
`potential`.*,
`customer`.`ID` as 'FID_customer'
FROM
`os_potential` as `potential`,
`os_customer` as `customer`
WHERE `potential`.`FID_author` = :randomID
AND `potential`.`converted` = 1
AND `potential`.`street` = `customer`.`street`
AND `potential`.`zip` = `customer`.`zip`
AND `potential`.`city` = `customer`.`city`;
Second sql:
SELECT
sum(`order`.`price_customer`) as 'Summe'
FROM
`os_order` as `order`,
`RESUTS_FROM_PREVIOUS_SQL_STATEMENT` as `results`
WHERE `order`.`FID_status` = 10
AND `results`.`FID_customer` = `order`.`FID_customer`;
I would like to get everything from first sql + the 'Summe' from second sql.
TABLES
1.Potentials:
+----+------------+-----------+--------+-----+------+
| ID | FID_author | converted | street | zip | city |
+----+------------+-----------+--------+-----+------+
2.Customers:
+----+--------+-----+------+
| ID | street | zip | city |
+----+--------+-----+------+
3.Orders:
+----+--------------+----------------+
| ID | FID_customer | price_customer |
+----+--------------+----------------+
SELECT p.*
, c.ID FID_customer
, o.summe
FROM os_potential p
JOIN os_customer c
ON c.street = p.street
AND c.zip = p.zip
AND c.city = p.city
JOIN
( SELECT FID_customer
, SUM(price_customer) Summe
FROM os_order
WHERE FID_status = 10
GROUP
BY FID_customer
) o
ON o.FID_customer = c.ID
WHERE p.FID_author = :randomID
AND p.converted = 1
;
You would just write a single query like this:
SELECT sum(o.price_customer) as Summe
FROM os_order o JOIN
os_potential p JOIN
os_customer c
ON p.street = c.street AND p.zip = c.zip AND p.city = c.city JOIN
os_order o2
ON o2.FID_customer = c.FID_customer
WHERE p.FID_author = :randomID AND p.converted = 1 AND
o2.FID_status = 10 ;
Notes:
Never use commas in the FROM clause. Always use explicit JOIN syntax with conditions in an ON clause.
Table aliases are easier to follow when they are short. Abbreviations for the table names is commonly used.
Backticks are only necessary when the table/column name needs to be escaped. Yours don't need to be escaped.
If the 1st query return 1 record per customer, then just simply join the 3 tables, keep the sum and use the group by clause:
SELECT
`potential`.*,
`customer`.`ID` as 'FID_customer',
sum(`order`.`price_customer`) as Summe
FROM
`os_potential` as `potential`
INNER JOIN
`os_customer` as `customer`
ON `potential`.`street` = `customer`.`street`
AND `potential`.`zip` = `customer`.`zip`
AND `potential`.`city` = `customer`.`city`
LEFT JOIN
`os_order` as `order`
ON `results`.`FID_customer` = `order`.`FID_customer`
AND `order`.`FID_status` = 10
WHERE `potential`.`FID_author` = :randomID
AND `potential`.`converted` = 1
GROUP BY `customer`.`ID`, <list all fields from potential table>
If the 1st query may return multiple records per customer, then you need to do the summing in a subquery:
SELECT
`potential`.*,
`customer`.`ID` as 'FID_customer',
`order`.Summe
FROM
`os_potential` as `potential`
INNER JOIN
`os_customer` as `customer`
ON `potential`.`street` = `customer`.`street`
AND `potential`.`zip` = `customer`.`zip`
AND `potential`.`city` = `customer`.`city`
LEFT JOIN
(SELECT FID_customer, sum(price_customer) as Summe
FROM `os_order`
WHERE FID_status=10
GROUP BY FID_customer
) as `order`
ON `results`.`FID_customer` = `order`.`FID_customer`
WHERE `potential`.`FID_author` = :randomID
AND `potential`.`converted` = 1
I think you should use a subselect, but be careful with the number of results, it's not the best for performance.
You can do something like this:
SELECT n1, n2, (select count(1) from whatever_table) as n3, n4 from whatever_table
note that the subselect must return just 1 result, in other case you'll have an error
Hi all I am having 3 tables as follows
Technology_table
Technology_ID Technology_Name
10 Asp.Net
20 C#
Question_table
QUESTION_ID Technology_ID QUESTION_DESCRIPTION
1 10 First ques in Asp.net
2 20 First ques in C#
Reply_table
QUESTION_ID Technology_ID Reply_Date Reply_Message
1 10 2016-01-23 I am first to post
1 10 2016-01-24 I am second to post
I have written the following query but not getting the result as expected
select
FI.QUESTION_ID,FI.QUESTION_TITLE,FI.USER_NAME,FI.DATE_POSTED,
FI.[DATE_REPLIED],FI.RepliedName,FI.VIEW_COUNT,FI.REPLY_COUNT,
FI.REPLY_MESSAGE,TT.TECHNOLOGY_ID,TT.TECHNOLOGY_NAME
from FORUM_TECHNOLOGY TT,
( select distinct
TQ.TECHNOLOGY_ID,TQ.QUESTION_ID,TQ.QUESTION_TITLE,TQ.USER_NAME,
TQ.DATE_POSTED,
TR.[DATE_REPLIED],
TR.USER_NAME as RepliedName,
TQ.VIEW_COUNT,TQ.REPLY_COUNT,TR.REPLY_MESSAGE
from FORUM_QUESTIONS TQ
LEFT OUTER JOIN FORUM_REPLIES TR
ON TR.TECHNOLOGY_ID=TQ.TECHNOLOGY_ID
and TR.QUESTION_ID = TQ.QUESTION_ID
and TR.[DATE_REPLIED] in (
select MAX(TR.[DATE_REPLIED])
from FORUM_REPLIES TR
group by TR.QUESTION_ID
)
) FI
where FI.TECHNOLOGY_ID =TT.TECHNOLOGY_ID
and TT.TECHNOLOGY_ID = #TechID
I also tried in this way
select t1.QUESTION_ID,oa.USER_NAME,oa.REPLY_MESSAGE
from FORUM_QUESTIONS t1
cross apply(select top 1 * from FORUM_REPLIES
where QUESTION_ID = t1.QUESTION_ID
order by DATE_REPLIED desc)oa
join FORUM_TECHNOLOGY t2 on oa.TECHNOLOGY_ID = t2.TECHNOLOGY_ID
AND oa.QUESTION_ID = t1.QUESTION_ID
I would like to display only one instead of duplicates
Can some one help me
This assumes that your DATE_REPLIED has a valid time component and isn't just omitted or defaulted to midnight.
SELECT FQ.QUESTION_ID
, FQ.QUESTION_TITLE
, FQ.USER_NAME
, FQ.DATE_POSTED
, FR.DATE_REPLIED
, FR.RepliedName
, FQ.VIEW_COUNT
, FI.REPLY_COUNT
, FR.REPLY_MESSAGE
, TT.TECHNOLOGY_ID
, TT.TECHNOLOGY_NAME
FROM FORUM_TECHNOLOGY AS TT
INNER JOIN FORUM_QUESTION AS FQ
ON FQ.TECHNOLOGY_ID = TT.TECHNOLOGY_ID
LEFT OUTER JOIN (
SELECT QUESTION_ID
, COUNT(*) AS REPLY_COUNT
, MAX(DATE_REPLIED) AS DATE_REPLIED
FROM FORUM_REPLIES
GROUP BY QUESTION_ID
) AS FI
ON FI.QUESTION_ID = FQ.QUESTION_ID
INNER JOIN FORUM_REPLIES AS FR
ON FR.QUESTION_ID = FI.QUESTION_ID
AND FR.DATE_REPLIED = FI.DATE_REPLIED
Could you please help me to make this query works
SELECT *
FROM `SC_orders`
LEFT JOIN `SC_customer_reg_fields_values` using(customerID)
WHERE (`statusID` = 2 OR `statusID` = 3 OR `statusID` = 21 OR `statusID` = 25 OR `statusID` = 26) AND DATE(order_time) > '2012-12-01 00-00-00'
LEFT JOIN `SC_ordered_carts`
ON orderID = orderID
GROUP BY orderID
I try to combine information from 3 tables in one output. This query works fine without last LEFT JOIN and Grouping. Where is my mistake?
The where needs to be after the last join. also, the second ON clause is ambiguous and I think the group by is unnecessary since you don't have any aggregate functions:
SELECT *
FROM `SC_orders`
LEFT JOIN `SC_customer_reg_fields_values` using(customerID)
LEFT JOIN `SC_ordered_carts` using(orderID)
WHERE (`statusID` = 2 OR `statusID` = 3 OR `statusID` = 21 OR `statusID` = 25 OR `statusID` = 26) AND DATE(order_time) > '2012-12-01 00-00-00'