I would like to obtain a sub-query's return row count in the query itself. Then I will use it in the main query in an if case. I have added an example and, as a dummy string I put QUERY_ROW_COUNT.Is there any function to achieve this? Thanks in advance...Regards...
SELECT period_id,
lt_id,
period_name,
min_stay,
IF(QUERY_ROW_COUNT=1, 1,0) as tag
FROM (SELECT period_id,
lt_id,
period_name,
min_stay,
fromDate,
toDate,
DATEDIFF( '2010-12-27', '2010-12-10' ) as totalDays,
nightly_rate,
case when ('2010-12-10' > fromDate AND '2010-12-27' < toDate) then
DATEDIFF( '2010-12-27', '2010-12-10' )
else 0
end as d6,
case when ('2010-12-10' > fromDate AND '2010-12-27' > toDate) then
DATEDIFF( toDate, '2010-12-10' )+1
else 0
end as d7,
case when ('2010-12-10' < fromDate AND '2010-12-27' < toDate) then
DATEDIFF( '2010-12-27', fromDate )
else 0
end as d8,
case when ('2010-12-10' < fromDate AND '2010-12-27' > toDate) then
DATEDIFF( toDate, fromDate )
else 0
end as d9
FROM `lt_hperiods`
WHERE ('2010-12-10' BETWEEN Date( fromDate ) AND Date( toDate ) )
OR ( '2010-12-27' BETWEEN Date( fromDate ) AND Date( toDate ))
OR ('2010-12-10' <= fromDate AND '201开发者_StackOverflow社区0-12-27' >= toDate )
AND (lt_id=1)) MQS
you could use count( 1 ) as some_variable
in the in-line select in your main from
clause, then refer to it as MQS.some_variable
in the select of your main query.
Have you tried COUNT(*) or COUNT(DISTINCT period_id) where you have QUERY_ROW_COUNT?
(SOLVED...this is the tag who is scanning a working solution:) Thanks for all suggestions. I forgot to write that I have already tried all sum and count functions.. I however solved my problem sth like this.. I hope This helps to somebody. mysql allows you to define dynamic variables that is great.. This is the reference:
http://www.xaprb.com/blog/2006/12/02/how-to-number-rows-in-mysql/
This is the code:
set @countT = 0;
set @rowCount = 0;
SELECT MQ.period_id, MQ.lt_id, MQ.period_name, MQ.min_stay, MQ.fromDate, MQ.toDate, MQ.nightly_rate, MQ.periodDays,
MQ.totalDays,
@countT := if(@countT = MQ.periodDays, 0 , @countT + MQ.periodDays) as periodDaysTotal,
@rowCount:= @rowCount +1
FROM (
SELECT period_id, lt_id, period_name, min_stay, fromDate, toDate, totalDays, nightly_rate, (d1+d2+d3+d4+d5+d6+d7+d8+d9) periodDays
FROM
(SELECT period_id, lt_id, period_name, min_stay, fromDate, toDate, DATEDIFF( '2010-05-02', '2010-03-10' ) as totalDays, nightly_rate,
case when ('2010-03-10' > fromDate AND '2010-05-02' < toDate) then DATEDIFF( '2010-05-02', '2010-03-10' ) else 0 end as d6,
case when ('2010-03-10' > fromDate AND '2010-05-02' > toDate) then DATEDIFF( toDate, '2010-03-10' ) else 0 end as d7,
case when ('2010-03-10' < fromDate AND '2010-05-02' < toDate) then DATEDIFF( '2010-05-02', fromDate ) else 0 end as d8,
case when ('2010-03-10' < fromDate AND '2010-05-02' > toDate) then DATEDIFF( toDate, fromDate ) else 0 end as d9
FROM `lt_hperiods`
WHERE
('2010-03-10' BETWEEN Date( fromDate ) AND Date( toDate ) ) OR
( '2010-05-02' BETWEEN Date( fromDate ) AND Date( toDate ))
OR ('2010-03-10' <= fromDate AND '2010-05-02' >= toDate )
AND (lt_id=1)
) MQS ) MQ
精彩评论