473,386 Members | 1,804 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,386 software developers and data experts.

Ordering select results by column

I have a table with three text columns and one time column (say text1,
text2, text3, and time). I wrote a query to select the rows where any
of
the text columns contains a certain keyword:
select * from table1 where
text1 like 'keyword' OR
text2 like 'keyword' OR
text3 like 'keyword';
Now I need to order the
results so that rows associated with text1 are listed first, then
text2,
then text3; and also have the results from each column sorted by
time. Is there a way to do this in mysql? Thanks!

Aug 22 '05 #1
3 1488
max
On 22 Aug 2005 01:42:35 -0700, mf*****@gmail.com wrote:
I have a table with three text columns and one time column (say text1,
text2, text3, and time). I wrote a query to select the rows where any
of
the text columns contains a certain keyword:
select * from table1 where
text1 like 'keyword' OR
text2 like 'keyword' OR
text3 like 'keyword';
Now I need to order the
results so that rows associated with text1 are listed first, then
text2,
then text3; and also have the results from each column sorted by
time. Is there a way to do this in mysql? Thanks!

Would it fit your scheme if you went with separate SELECTs -

select * from table1 where text1 like 'keyword' order by time
select * from table1 where text2 like 'keyword' order by time
select * from table1 where text3 like 'keyword' order by time

I suppose you could either print them out as the query runs, or stick them
into arrays???
Aug 22 '05 #2
max wrote:
select * from table1 where text1 like 'keyword' order by time union all select * from table1 where text2 like 'keyword' order by time union all select * from table1 where text3 like 'keyword' order by time


Regards,
Bill K.
Aug 22 '05 #3
>I have a table with three text columns and one time column (say text1,
text2, text3, and time). I wrote a query to select the rows where any
of
the text columns contains a certain keyword:
select * from table1 where
text1 like 'keyword' OR
text2 like 'keyword' OR
text3 like 'keyword';
Now I need to order the
results so that rows associated with text1 are listed first, then
text2,
then text3; and also have the results from each column sorted by
time. Is there a way to do this in mysql? Thanks!


You can order by an expression. Now construct an expression that
gives you the order you want.

order by
if(text1 like 'keyword', 1, if (text2 like 'keyword', 2,
if (text3 like 'keyword', 3, 4))),
time

This presumes that there aren't any rows where more than one column
matches, or if there are, it is ordered like it would be if only the
first matching column counted.

You may want to do the query as:
text1 like '%keyword%'
or text1 like '% keyword %'
depending on how you lay out your columns.
text like 'key' matches only 'key'.
text like '%key%' matches 'key' and 'keyword' and 'monkey'.
text like '% key %' matches ' key ' and ' gerbil key hamster ' but
not ' monkey '.
but if you want the first or last keywords to match, you need leading
and trailing spaces in the column.

Gordon L. Burditt
Aug 22 '05 #4

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

Similar topics

4
by: R.G. Vervoort | last post by:
Does anyone have a suggestion how I can order a list with names in a secondary select string. The first string selects a number of locations where people work From this string I get...
2
by: Ken Fine | last post by:
(originally posted to one of macromedia's groups; no help, so hopefully someone here can help me out. I'm using VBScript ASP.) When designing administrative interfaces to websites, we often need...
4
by: blue | last post by:
Is there a preferred way to order a varchar column numerically (for those that are numeric), then alphanumerically for all others? I've tried: ORDER BY CASE WHEN IsNumeric(<column_name>) = 1...
9
by: Rick | last post by:
I've created a clustered two column index on a table where the second column is an integer value. When the first column is the same, instead of ordering in numerical order it is ordering...
3
by: Hadley Willan | last post by:
Hi, I was wondering if it's possible to order the result set by some of the set contained in an IN clause. For example. SELECT * FROM v_fol_unit_pub_utmpt WHERE folder_folder_object = 100120...
1
by: Matt Roberts | last post by:
Please accept my apologies if this is answered elsewhere in the archives or docs but I have searched without luck. I've always assumed that default ordering of selects are based on a first in...
20
by: Brian Tkatch | last post by:
An ORDER BY a simple-integer inside a FUNCTION, results in SQL0440N, unless the FUNCTION expects an INTEGER as its parameter. For example: DECLARE GLOBAL TEMPORARY TABLE A(A CHAR(1)) INSERT INTO...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
5
by: Joe Molloy | last post by:
Hi Group, I have a task and I'm interested to see what ideas people have here for the best way of tackling it. I have a table of products. Each of the products is associated with a certain...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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,...
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...

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.