Jump to content

SELECT * (start from middle of results)


rubing

Recommended Posts

I read the mysql manual page on sorting results, but couldn't find an answer to what i am trying to do.

 

I have an alphanumeric list of stuff, which I list one item at a time (mysql pagination).  However, I want the starting point based on user input.  The user should also be able to index back or forward in the now alphabetized list respective to his starting point. 

 

Is there a simle way to do this?  thanks!

Link to comment
Share on other sites

That is exactly what i am trying to do, however I don't know where to start the internal pointer.

 

For example:

If the users query is 'spaghetti' I have absolutely no idea which result # it is out of all my results.  In other words where to start the LIMIT statement.

Link to comment
Share on other sites

First, decide how many results/page (rpp), i'm going to use 20 for this example. Now, you will need to keep track of the 'page' they are on through a GET variable.

 

We calculate the start with this: (page - 1) * rpp

 

Page 1

(1-1)*20 = 0

SQL: SELECT * FROM `tablename` LIMIT 0,20

 

Page 2

(2-1)*20 = 20

SQL: SELECT * FROM `tablename` LIMIT 20,20

 

etc

Link to comment
Share on other sites

I understand that (i am only using 1 per page).  BUT how do you know which page to start on  ???

 

Let me try to explain better. 

 

My database has a thousand items, which are alphanumeric.  I want the user to input text (query) in order to get the nearest matched item (alphabetically) to his query.  So, If he types spaghetti...I need someway of knowing that the result (s)he sees is the 354th item in this last, when they are sorted alpabetically.

 

Does that make sense? 

 

Furthermore, I am running this as a commandline autoresponder email script and so want to do this as succintly as possible.  Programming this way is very prone to errors and bounced emails.

Link to comment
Share on other sites

Well, the following should always return the same result set (unless you alter your data):

 

SELECT * FROM `tablename` WHERE `fieldname` = 'spaghetti'

 

So, to get the first one:

SELECT * FROM `tablename` WHERE `fieldname` = 'spaghetti' LIMIT 0,1

Second one:

SELECT * FROM `tablename` WHERE `fieldname` = 'spaghetti' LIMIT 1,1

etc

 

Instead of tracking what specific record they are looking at, you want to track the search criteria, and what row of the returned dataset you want. So, row 1 is the start (which is index 0), row 2 is next, etc.

 

Sorry if I'm not explaining this well...it makes sense in my head, I'm just having trouble putting it in writing.

Link to comment
Share on other sites

But I want to allow them to index through every record in my table.  Not just the ones where

 

'fieldname' = 'spaghetti'

 

I just want to start the index at spaghetti. Then let them see the previous or next record if they desire.

Link to comment
Share on other sites

Well, I guess there are two possible solutions then.  First Fenway's solution would involve:

 

Creating a virutal or copy of my table which I first sort ASC and then add an ID field with auto increment.  Then, whenever I retrieve a result I can refrence the records ID number.

 

Barand's solution looks a little more elegant, but I don't know if I completey understand the syntax.  I see that you are using the count function to figure out which # result spaghetti is.  COOL!!

 

But, I am not sure about your use of (a.*) or (a)  ???

 

 

Link to comment
Share on other sites

The "a" is an alias for the tablename you're using. In other words, its a shortcode for the table. So instead of writing myreallylongtablenamethatistotallystupid.column1 you set up an alias for the table and reference that instead (i.e. a.column1)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.