Order Results of search query mySQL - mysql

I have the following mysql query for a search function. I have in my data 3 different account types that I need to show in a specific order. For that I used ORDER BY FIELD(profiltype,2,1,3). That works great and with no problems, however within these results I also need to order the different columns in a priority like:
1. name2
2. a_z_feld1
etc.
However this is not working with the query I have below. It should keep the order of Profile types but within this order also have a priority of the columns.
SELECT * FROM main_users
WHERE ((name2 LIKE %s)
OR (a_z_feld1 LIKE %s)
OR (a_z_feld2 LIKE %s)
OR (a_z_feld3 LIKE %s)
OR (city LIKE %s)
OR (content_stellenvor LIKE %s)
OR (main_content LIKE %s))
AND levelmember <> 1
ORDER BY FIELD(profiltype,2,1,3) ASC,
a_z_feld1 ASC, a_z_feld2 ASC, a_z_feld3 ASC

Based on your comment, you need to include name in the order by:
ORDER BY FIELD(profiltype,2,1,3) ASC, name2, a_z_feld1 ASC, a_z_feld2 ASC, a_z_feld3 ASC

Related

Order by LOCATE MySql

I'm trying to retrieve the data set by locating a string based on its occurrence, check the below query -
select name, LOCATE('test', name)
from afkapi_dev.articles
where name like '%test%'
order by LOCATE('test', name) ASC
The result set is -
Result set is perfectly fine, but the issue here is the ordering.
Can anyone explain why "testing" is listed above "test low" and also suggest a way to make this happen.
Thank you
You seem to be asking to do further sorting on name. For multiple level sorting, you can specify different columns/expressions in ORDER BY clause, separated by comma, with respective sorting order [ASC or DESC]
Also, you can alias the LOCATE(..) expression in the SELECT clause, and reuse that in the ORDER BY. This should prevent re-computation of LOCATE(..) values.
select name, LOCATE('test', name) AS location
from afkapi_dev.articles
where name like '%test%'
order by location ASC, name ASC

MySQL ORDER BY + LIMIT + OFFSET statements: how to OFFSET firts and only then sort with ORDER BY

I have a table which consists of ID, NAME, PRICE and DATE columns.
I'm trying to write a pager-style navigation because there are plenty of entries in the table, so looking at the whole SELECT's output had become uncomfortable.
I've written the following request:
SELECT id
, name
, price
, date
FROM mytable
ORDER
BY name asc
LIMIT 30 OFFSET _here's_my_offset_depending_on_the_current_position_
This works fine only as in the example. When I try to sort it, say, by price, it seems that MYSQL first sorts the whole table with ORDER BY and only after it makes an offset.
How do I change this behavior, in other words how do I make an offset and only than sort the resulting rows by whaterver I like?
It's can be easy if you use subquery:
SELECT * FROM (
SELECT id, name, price, date FROM mytable LIMIT 30 OFFSET _offset_
) AS page ORDER BY page.name asc

Group different groups as the same and count?

I have the following query:
SELECT owner,
typeOwner,
type,
count(*) AS count
FROM myTable
GROUP BY typeOwner
ORDER BY count DESC
LIMIT 0, 30
Now , the typeOwner values = '<test>a</test>' but , some times the typeOwner field will have some string else like '<test>b</test>, how can i let this query count the <test>b</test> as a group of '<test>a</test>.
I want to make an exception for this, I mean typeOwner <test>a</test> AND typeOwner <test>b</test> should be counted as one row that have two count.
here's a fiddle : http://sqlfiddle.com/#!2/70053/1 , take look
actually these are should be grouped by <type></type><aId></aId>
"actually these are should be grouped by "
You can GROUP BY something relatively ugly like:
GROUP BY LEFT(typeOwner, position('<xType>' in typeOwner) -1)
But you are probably better off preprocessing the data in some fashion. I'm not sure how MySQL handles xml, but in SQL server I might extract the XML values into first class relation fields if I needed to do this sort of processing with any frequency.
sqlfiddle.com
Based on your sqlfiddle, this works
SELECT
left(typeOwner, instr(typeOwner, '<xType>')-1) as typeOwner,
owner,type, count(*) as count
FROM `test`
GROUP BY left(typeOwner, instr(typeOwner, '<xType>')-1)
ORDER BY count DESC
LIMIT 0 , 30

ORDER BY FIELD() and duplicate data

Lets say I have the following query.
SELECT stringdata FROM table ORDER BY FIELDS( stringdata, 'tg','nk','mg','pl') asc;
For some reason I'm getting the results at the very bottom. How can I get the query to put the results starting from 'tg' at row 1 rather than the last row in the results?
Not only that but there's more than one 'tg' in the data, I'd like it to sort it in this expected output:
stringdata
__________
'tg'
'tg'
'tg'
'nk'
'nk'
'mg'
'mg'
'mg'
'pl'
So far using ORDER BY Fields() is only sorting one instance of the data rather than all.
Using desc instead of asc in the query works as expected. I get 'pl' on the first row, then 'mg', 'nk', etc.
Normally the FIELD clause in ORDER BY works something like
SELECT * FROM table ORDER BY FIELD(field, high_priority, second_high,
,....., low_priority);
So in your query, the sorting took place as you mentioned and when you gave the ASC it printed from the lowest_priority. So, for your case, if you want tg at the top, you can either reorder the priority in the FIELDS or as you have already tried use desc
If you want first rows with 'tg', then rows with .... then with 'pl' and then all the rest sorted (ASC or DESC) use this:
SELECT stringdata
FROM table
ORDER BY FIELD( stringdata, 'pl','mg', 'nk', 'tg') DESC
, stringdata ASC --- or DESC

Method to sort MYSQL results by an arbitriary letter (ie, show all rows starting with F first)

I have a column of states, and, depending on the query, I want to order by results by a particular state, then by id (Asc or Desc, depending). For instance, I might want to show all rows with state "HI", sorted by ID desc, and then all the other rows, sorted by id desc.
I was hoping I could do this in one query, rather than getting all my favored state results, and then getting the rest. Can I?
How about:
SELECT id, state
FROM sometable
ORDER BY IF(state = 'HI', 0, 1) ASC, id DESC;
This will sort 'HI' rows first. If you want them last, change the ASC to DESC.
You have two options:
do a union
write a function and use it to order rows by
In the first case, you could do something like
select 1 as res_order, ...
...
where state like 'hi%'
union
select 2 as res_order, ...
...
where state not like 'hi%'
order by res_order asc, id desc
In the second case, you could do something like
select my_function(state, "hi") as row_order, ...
...
order by row_order
where the function returns lower values for matching states.
The code is off the top of my head: it might need some tweaking to make it runnable.