Unix Technical Forum

Column element alphabetically before and after given element

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


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-29-2008, 10:40 AM
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-29-2008, 10:40 AM
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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-29-2008, 10:40 AM
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 ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-29-2008, 10:40 AM
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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-29-2008, 10:40 AM
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
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:09 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com