If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Return x number of empty rows on purpose

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-21-05, 14:04
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
Return x number of empty rows on purpose

Hi,

It is possible to return a calculated number of empty rows that I can UNION join, with the number of empty rows calculated based on the number of rows returned in the other query? I always want to return 20 rows, and if the number of rows from the first query is less than 20 then the rest will be blank.
I though of doing this with a calculated LIMIT but I get an error.

SELECT a,b,c
FROM table1
WHERE d>10
UNION
SELECT '' AS a, '' AS b, '' AS c
FROM table1
LIMIT 20 - (SELECT COUNT(*) FROM table1 WHERE d>10)
Reply With Quote
  #2 (permalink)  
Old 10-21-05, 14:57
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
You cannot use expressions in a LIMIT clause.

Depending on your data, the following trick might work:

Code:
(
SELECT a,b,c
FROM table1
WHERE d>10
) UNION ALL (
SELECT '' AS a, '' AS b, '' AS c 
FROM table1
)
ORDER BY a DESC LIMIT 20;
--
felix
Reply With Quote
  #3 (permalink)  
Old 10-21-05, 15:19
jfulton jfulton is offline
Registered User
 
Join Date: Apr 2005
Location: Baltimore, MD
Posts: 297
Not sure how MySQL optimizes searches, but if the table is large, I'd assume it's probably not a bad idea to add a limit in the second select statement to prevent a full table scan.


Code:
(
SELECT a,b,c
FROM table1
WHERE d>10
) UNION ALL (
SELECT '' AS a, '' AS b, '' AS c 
FROM table1
LIMIT 20
)
ORDER BY a DESC LIMIT 20;
Reply With Quote
  #4 (permalink)  
Old 10-21-05, 15:45
PolarBear2k PolarBear2k is offline
Registered User
 
Join Date: Jun 2005
Posts: 79
Thumbs up

Works great! Even without the ORDER BY, just LIMIT by itself

Can't thank you enough.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On