473,809 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LIMIT clause question

Hi,

I need to retrived a specific row from a query, these these specific
row number is the result of another query.

Is it possible to do that in one query with a subquery (mysql 4.1.x)?
Here is how I try (that gave me a syntax error.):

SELECT a FROM t1 WHERE a = 1 ORDER BY LIMIT (SELECT b FROM t2 WHERE c
= 2),1;

############
the 2 query standalone worked.

SELECT b FROM t2 WHERE c = 2; # return 3

So:
SELECT a FROM t1 WHERE a = 1 ORDER BY LIMIT 3,1; # return the good row
Any clue.
Thanks
Troy

Jul 20 '05 #1
2 1629
troy wrote:
I need to retrived a specific row from a query, these these specific
row number is the result of another query.

Is it possible to do that in one query with a subquery (mysql 4.1.x)?


See http://dev.mysql.com/doc/mysql/en/SELECT.html:
"LIMIT takes one or two numeric arguments, which must be integer constants."

I take that to mean that the arguments _cannot_ be expressions of any
type. So I don't think you can use a subquery in place of those
arguments, or any expression involving a field name from the
select-list, etc.

I think you're stuck using one query to fetch the row number value, and
then use that return value when building up the SQL string for the
second query.

Regards,
Bill K.
Jul 20 '05 #2
On Mon, 15 Nov 2004 14:56:42 -0800, Bill Karwin <bi**@karwin.co m>
wrote:
troy wrote:
I need to retrived a specific row from a query, these these specific
row number is the result of another query.

Is it possible to do that in one query with a subquery (mysql 4.1.x)?


See http://dev.mysql.com/doc/mysql/en/SELECT.html:
"LIMIT takes one or two numeric arguments, which must be integer constants."

I take that to mean that the arguments _cannot_ be expressions of any
type. So I don't think you can use a subquery in place of those
arguments, or any expression involving a field name from the
select-list, etc.

I think you're stuck using one query to fetch the row number value, and
then use that return value when building up the SQL string for the
second query.

Regards,
Bill K.


I think you right, I will continue to use 2 distrincts queries.
Thanks
Troy
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
17103
by: Tamer Higazi | last post by:
Hi! I have problems creating joins between tables and to limit rows for a specified clolumn. I have 2 tables! employees departments I face the problems with the GROUP BY clause and i don't get it handled solving that problem. Neither with oracle sql or SQL1999 syntax!
10
14054
by: Randell D. | last post by:
Folks, I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no errors). The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db). Where am I going wrong? Because my select uses hashes (which I provide with
1
3049
by: Steve | last post by:
I've run in to a problem with a query I'm trying to write. I have attached a sample SQL script at the end of this post to show an overview of what I'm working with. I want to be able to use LIMIT to control how many rows from one table are returned, independent of how many rows there are in a second table that is joined to the first. When I execute the following SQL script:
3
2628
by: Jay K | last post by:
Hi, I have multiple queries like this: SELECT col1, col2, col3, col4 FROM table1, table2 where table1.col1 = table2.col1 and table1.col2 = 1 ORDER BY col3 desc LIMIT 5 and
5
2114
by: Jeremy | last post by:
I am relatively inexperienced with SQL, and I am trying to learn how to analyze some data with it. I have a table with the following information. COMPANY ID , DATE, MarektValue I would like to select the largest 100 marketvalues for each date in my table. How can I do this efficiently in MySql.
2
18119
by: Urban | last post by:
hi, I have a question. Maybe You know the equivalent to command LIMIT from MySQL I couldn`t find something like this in MS SQL PS I try to display 10 records begining form e.g. 4 sort by id something like: "SELECT * FROM table WHERE name=... LIMIT 4, 10 ORDER BY id" in MySQL
2
1760
by: Mike | last post by:
New to PHP and MySQL. Using PHP5 and MySQL 4.1 Windows XP Pro IIS 5.1 I'm trying to page a recordset, and am using a LIMIT clause to fetch a defined range of records from my db. However, the returned dataset is not limited to the range I have in the SQL clause. Here's the code:
1
2332
by: lawrence k | last post by:
Want to replace the limit clause in a query, but can't get it right. What's wrong with this: $pattern = "(.*)limit (.*)"; $replacement = '$1'; $replacement .= "LIMIT $limit"; $replacement .= '$2'; $query = preg_replace ($pattern, $replacement, $query);
1
7160
by: Franc Walter | last post by:
Hello, i didn't find it in the help, i think it is not possible, but i try to question anyway: Is the MySQL command "LIMIT" possible with a subquery in MySQL 4.0.27? e.g.: SELECT * FROM tab1 LIMIT (SELECT COUNT(*) from tab2) this doesn't work. MySQL 5.1 writes: User variables may be used in contexts where expressions are allowed.
1
22794
by: henryrhenryr | last post by:
Hello! I'm having a bit of trouble with a query. I am trying to paginate a set of results using LIMIT. Each result has a bit of 'basic' data and potentially multiple 'additional' data. I have used LEFT JOIN to get the data from table 2. I want to limit the amount of data to 10 rows of table 1 but if there are two pieces of data on table 2 for every row on table 1, I only get 5 rows of table one (ie 10 rows in all but not what I want). ...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10635
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10378
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7653
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.