Column element alphabetically before and after given element

This is a discussion on Column element alphabetically before and after given element within the MySQL Database forums, part of the Database Forums category; I haven't been able to write a SQL statement for this seemingly trivial selection. If column col of table ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-25-2008
thelma@uwm.edu
 
Posts: n/a
Default Column element alphabetically before and after given element

I haven't been able to write a SQL statement for this seemingly
trivial selection.

If column col of table T is [a,b,c,d,e]
how do I write

select * from T where col < 'c' limit 1
select * from T where col > 'c' limit 1

as a single query? Thanks,

--thelma
Reply With Quote
  #2 (permalink)  
Old 05-25-2008
Paul Lautman
 
Posts: n/a
Default Re: Column element alphabetically before and after given element

thelma@uwm.edu wrote:
>I haven't been able to write a SQL statement for this seemingly
> trivial selection.
>
> If column col of table T is [a,b,c,d,e]
> how do I write
>
> select * from T where col < 'c' limit 1
> select * from T where col > 'c' limit 1
>
> as a single query? Thanks,
>
> --thelma


SELECT
*
FROM t
WHERE col <> 'c'
LIMIT 1


Reply With Quote
  #3 (permalink)  
Old 05-25-2008
toby
 
Posts: n/a
Default Re: Column element alphabetically before and after given element

On May 25, 6:04 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
> the...@uwm.edu wrote:
> >I haven't been able to write a SQL statement for this seemingly
> > trivial selection.

>
> > If column col of table T is [a,b,c,d,e]
> > how do I write

>
> > select * from T where col < 'c' limit 1
> > select * from T where col > 'c' limit 1

>
> > as a single query? Thanks,

>
> > --thelma

>
> SELECT
> *
> FROM t
> WHERE col <> 'c'
> LIMIT 1


Don't you mean ORDER BY col LIMIT 2 ?
Reply With Quote
  #4 (permalink)  
Old 05-25-2008
Paul Lautman
 
Posts: n/a
Default Re: Column element alphabetically before and after given element

toby wrote:
> On May 25, 6:04 pm, "Paul Lautman" <paul.laut...@btinternet.com>
> wrote:
>> the...@uwm.edu wrote:
>> >I haven't been able to write a SQL statement for this seemingly
>> > trivial selection.

>>
>> > If column col of table T is [a,b,c,d,e]
>> > how do I write

>>
>> > select * from T where col < 'c' limit 1
>> > select * from T where col > 'c' limit 1

>>
>> > as a single query? Thanks,

>>
>> > --thelma

>>
>> SELECT
>> *
>> FROM t
>> WHERE col <> 'c'
>> LIMIT 1

>
> Don't you mean ORDER BY col LIMIT 2 ?


I don't think that would help. The order that the two single queries supply
their output is indeterminate, so there is no guarantee which two rows would
have been returned. Your adaptation would produce a and b, whereas the
second of the two posted queries whould have produced one of c or d.


Reply With Quote
  #5 (permalink)  
Old 05-25-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Column element alphabetically before and after given element

thelma@uwm.edu wrote:
> I haven't been able to write a SQL statement for this seemingly
> trivial selection.
>
> If column col of table T is [a,b,c,d,e]
> how do I write
>
> select * from T where col < 'c' limit 1
> select * from T where col > 'c' limit 1
>
> as a single query? Thanks,
>
> --thelma


Not tested (and may not work as listed, but close):

SELECT col
FROM T
WHERE col < 'c'
ORDER BY col DESC
LIMIT 1
UNION
SELECT col
FROM T
WHERE col < 'c'
ORDER BY col ASC
LIMIT 1

Notes:

Data in SQL databases is always unordered. If you want sorted data, you
must use ORDER BY in your SQL statement.

You should never use SELECT *. Always identify the columns you are
selecting.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
Reply


Thread Tools
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

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



All times are GMT +1. The time now is 04:58 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0