473,765 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1505
max
On 22 Aug 2005 01:42:35 -0700, mf*****@gmail.c om 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
2063
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 several id's form people (in the location table there
2
587
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 to provide users with a mechanism to change the order of items that are listed on the page.For example, the _New York Times_ website (http://www.nytimes.com) lists a bunch of top news articles, and normally these are ordered by purely mechanical...
4
4765
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 THEN CONVERT(Float, <column_name>) ELSE 999999999 END; and
9
2075
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 1,10,100,2,20,200 etc. How can I change the behaviour to order the data in numerical order? Thanks, Rick
3
1453
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 AND unit IN ( 90072, 90005, 90074, 90075 ) AND unit_pub_type IN ( 2 ) AND utmpt IN ( 1 ); Results in.
1
5860
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 first out principle and that this remains true at the row level despite edits to columns. However I'm dealing with a case where this doesn't seem to hold true. The ordering has changed over time. Its difficult for me to gauge whether the data has...
20
2501
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 SESSION.A VALUES ('a'), ('b') CREATE FUNCTION A(A char(1)) RETURNS char(1) DETERMINISTIC NO EXTERNAL ACTION RETURN A SELECT A FROM SESSION.A ORDER BY A(1) DROP FUNCTION A DROP TABLE SESSION.A
6
4849
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 SalesManName AT Alan Time
5
1241
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 section which is achieved with a foreign key (the section id) linked to the sections table. Now the problem is that when the products are listed in each section the
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.