473,396 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to return the record above and the record below .. ?

106 100+
So I have this table:

------------------
Time --- Result
- - - - - -
11am - Yes
13pm - No
17pm - Yes
-------------------


I have a php script that when a user searches for 11am they get that line.
That's easy right?

But what If the user doesn't know the exact time? I want to create a php/mysql script that If a user searches for 12pm they get 11pm and 13pm, as those are the record above and the record bellow what the user searched.

Can this be done? How?

Thanks ;)
Aug 22 '10 #1

✓ answered by Jerry Winston

The
Expand|Select|Wrap|Line Numbers
  1. as SubQ1
creates an alias, just another name to refer to our
Expand|Select|Wrap|Line Numbers
  1. (SELECT Field1 FROM myTable WHERE Field1 < searchVar ORDER BY Field1 ASC)
sub query. The same goes for
Expand|Select|Wrap|Line Numbers
  1. as SubQ2
I just spotted an error in my code. My sub queries (SubQ1 and SubQ2) were missing their SELECT statements.
Expand|Select|Wrap|Line Numbers
  1. SELECT Field1 FROM
  2. (SELECT Field1 FROM myTable WHERE Field1 < searchVar ORDER BY Field1 ASC) as SubQ1
  3. LIMIT n
  4.  
  5. UNION
  6.  
  7. SELECT Field1 FROM
  8. (SELECT Field1 FROM myTable WHERE Field1 > searchVar ORDER BY Field1 DESC) as SubQ2
  9. LIMIT n
  10.  

9 2521
code green
1,726 Expert 1GB
There is no such thing as next record. last record or record above or below in a table.

You can simulate this with a query that has an ORDER BY, then above and below are the next/ last recordset.

so I suggest modifying the query.
You could use DATEDIFF(HOUR,-1(+1),timefield) to help you
Aug 23 '10 #2
londres9b
106 100+
Oh, ok.

But let's forget that the first field is about date and time. Let's say it's just a normal text field.

Isn't there a way to do this:
If there is no record that corresponds to what the user searched, give back the next approximate record?
Like the user searched 5 but there is no 5 and so the script searches for 3 and 4, 2 and 5 ..

thanks :)
Aug 23 '10 #3
Jerry Winston
145 Expert 100+
What kind of proximity are you looking for?
  • Are you looking for an ASCII value range?
  • Are you looking for a date range for date values stored as text?
  • Are you looking for a "n row above, n row below" solution based on a sort?

We can't "just forget" the data type because a record is only above or below any other record because of a particular sort order based on a field or set of fields who's sort behavior is tied to data type.
Aug 23 '10 #4
londres9b
106 100+
Well, first I want the script to go and see if there is a record that corresponds to what searched for.

And if not maybe check the 'value the user searched' + 1 and -1 ...

Can you explain the "n row above, n row below" please?
Aug 23 '10 #5
Jerry Winston
145 Expert 100+
If you know how to get the n rows above you also know how to get the n rows below. It's just the UNION of two queries:

Expand|Select|Wrap|Line Numbers
  1. SELECT Field1 FROM
  2. (Field1 FROM myTable WHERE Field1 < searchVar ORDER BY Field1 ASC) as SubQ1
  3. LIMIT n
  4.  
  5. UNION
  6.  
  7. SELECT Field1 FROM
  8. (Field1 FROM myTable WHERE Field1 > searchVar ORDER BY Field1 DESC) as SubQ2
  9. LIMIT n
  10.  
You'd be putting together the n records < searchVar with the n records > searchVar. My MySQL is quite rusty these days so the syntax may be off but that's the general logic.

I could be wrong, but I'm pretty sure you already knew how to get n rows below or above a value before you came here. It's not always fun but defining a troublesome data set using set notation first usually saves you time in the end as you already know the basic operations to bound the subsets of target data set.
Aug 23 '10 #6
londres9b
106 100+
I'm sorry but I am a newbie to MySql...

SELECT Field1 FROM
(Field1 FROM myTable WHERE Field1 < searchVar ORDER BY Field1 ASC) as SubQ1
LIMIT n
I think I understand everything besides SubQ1.. ?
Aug 23 '10 #7
Jerry Winston
145 Expert 100+
The
Expand|Select|Wrap|Line Numbers
  1. as SubQ1
creates an alias, just another name to refer to our
Expand|Select|Wrap|Line Numbers
  1. (SELECT Field1 FROM myTable WHERE Field1 < searchVar ORDER BY Field1 ASC)
sub query. The same goes for
Expand|Select|Wrap|Line Numbers
  1. as SubQ2
I just spotted an error in my code. My sub queries (SubQ1 and SubQ2) were missing their SELECT statements.
Expand|Select|Wrap|Line Numbers
  1. SELECT Field1 FROM
  2. (SELECT Field1 FROM myTable WHERE Field1 < searchVar ORDER BY Field1 ASC) as SubQ1
  3. LIMIT n
  4.  
  5. UNION
  6.  
  7. SELECT Field1 FROM
  8. (SELECT Field1 FROM myTable WHERE Field1 > searchVar ORDER BY Field1 DESC) as SubQ2
  9. LIMIT n
  10.  
Aug 23 '10 #8
londres9b
106 100+
It's not working.. I had the values 1, 2, 4, 5 and I made so that the searchVar was 3 and what I got was all the rows.. (using LIMIT 2 i got the 1st 2 rows) ..
Aug 24 '10 #9
londres9b
106 100+
Sorry, my mistake.. I got it!
Aug 24 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew | last post by:
Hi All, Have come across something weird and am after some help. Say i run this query where rec_id is a column of table arlhrl, select * from arlhrl where rec_id >= 14260 This returns to...
3
by: Eric | last post by:
Is it possible to set up a <ul> with <li> and now have the default blank line above and below the list itself? Thanks, Eric
5
by: MLH | last post by:
Anyone remember how to determine a particular record's record number (Access 2.0 table question)?
1
by: David | last post by:
I have a form on which i choose a record, then from this I open another form for updating data. On closure of the second form I requery the data which sets the first form back to the first record....
1
by: sal21 sal21 | last post by:
I use this code to update a sql database from excel to sql... Now i would want to arange this code to export an Access table into sql databse... Sub ADOExcelToSQLServer() Dim Cn As...
2
by: javakid | last post by:
How can i reduce the space above and below list? Is their any way i can write some attribute in <ul> ? Any suggestions Regards
14
seeoneomen
by: seeoneomen | last post by:
I am having a spacing problem above and below my divs that are positioned "relative". In FireFox space is added above and below my div whereas the page displays correctly in IE on the PC. If you view...
2
by: glynester | last post by:
I have some code that works off the update event of a textbox field (which is a part of a record in a continuous form). If certain conditions are met (following the updating of the field), I want a...
6
by: SethM | last post by:
I have a stored procedure that returns a record set. I want to functionalize this so I can have multiple presentations of the same record set. However, I can not get rs_event.open StoreProc to pass...
13
by: stateemk | last post by:
I have a form that will have about 50 - 100 records entered at a time all needing the same batch number. All the other data will need to change, but I would like to be able to retain that batch...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.