I have this Select:
SELECT pts_name
FROM products_tecspecs
WHERE pts_id IN ( 5275, 21, 5276, 5277,
5278, 49, 5279 )
ORDER BY FIELD (pts_id, 5275, 21, 5276, 5277, 5278, 49, 5279)
I am trying to get the result and order it the way I want. I found this answer but got this error: #1305 - FUNCTION database_name.FIELD does not exist
I am trying to get specifics results with IN (that's working), but sorting the way I want.
Help you be apreciated.
Remove the space between the function and the parenthesis: FIELD (
MySQL parser does not like spaces there. Use:
FIELD(pts_id, ...)
you can also try subquery to order by records in custom order
SELECT pts_name
FROM (SELECT pts_name,
CASE pts_id
WHEN 5275 THEN 1
WHEN 21 THEN 2
WHEN 5276 THEN 3
WHEN 5277 THEN 4
WHEN 5278 THEN 5
WHEN 49 THEN 6
WHEN 5279 THEN 7
END AS sort_order
FROM products_tecspecs
WHERE pts_id IN ( 5275, 21, 5276, 5277, 5278, 49, 5279)
) a
ORDER BY a.sort_order ASC ;
Related
I am trying to perform a weighted sort on one of the columns (PrefId) of my view. I came across Order By FIELD for that.
This is my exact query:
select
e.employeeName as empName,
e.employeeLogin as empAlias,
e.managerName as mangerName,
e.managerLogin as managerAlias,
b.buildingName as buildingName,
b.country as country,
b.region as region,
CASE WHEN w.preferenceId IS NULL THEN 0 ELSE w.preferenceId END as PrefId,
CASE WHEN w.lastUpdatedDate IS NULL THEN e.lastUpdatedDate ELSE w.lastUpdatedDate END as updateDate
FROM employee_details e
INNER JOIN buildings_details as b ON e.building = b.building
LEFT JOIN workplace_details w ON e.employeeId = w.employeeId
WHERE e.isWorkplacePrefScopeIncluded = 1
ORDER BY FIELD(PrefId, 4, 3, 99, 2, 1, 0);
The query works fine on my workbench and the result is as expected but upon executing the same through hibernate gives
org.hibernate.engine.jdbc.spi.SqlExceptionHelper: Unknown column 'PrefId' in 'order clause'
I also tried with - ORDER BY FIELD(w.preferenceId, 4, 3, 99, 2, 1, 0) but that doesn't seem to honour the weighted sort order.
Where am I going wrong??
You actually seem to have two problems here. First, PrefId is an alias which you defined in the select clause earlier in the query. Hibernate seems to be complaining about this, so just use preferenceId instead. Second, FIELD is a MySQL extension and is not part of the ANSI standard, nor is it part of the Hibernate query language. You may however refactor your ORDER BY clause:
ORDER BY FIELD(PrefId, 4, 3, 99, 2, 1, 0);
to this, using a CASE expression:
ORDER BY
CASE COALESCE(w.preferenceId, 0) WHEN 4 THEN 1
WHEN 3 THEN 2
WHEN 99 THEN 3
WHEN 2 THEN 4
WHEN 1 THEN 5
WHEN 0 THEN 6
ELSE 7 END
Not sure how to explain this. I have the following sample table
id participants
13 128, 125
18 122, 125
29 182, 125
34 17, 12
38 18, 15
I want to get a count of the messages where 12 is on the right so am using the following query, select count(id) as messages from table where locate (12, participants, 2)
The problem with this query is that it returns all results with 12 e.g. 125, so instead of having one count i have 4 counts which is incorrect. Any suggestions on what query to use?
You can get your result with the help of SUBSTRING_INDEX and ', ' as delimiter:
SELECT * FROM your_table
WHERE
SUBSTRING_INDEX(participants, ', ', -1) = 12;
If you can't be sure about the blank after the comma, you could use
SELECT * FROM your_table
WHERE
TRIM(SUBSTRING_INDEX(participants, ',', -1)) = 12;
instead.
Demo
Note
It would be better not to store delimited lists of values in just one field, if you can. It's asking for trouble and bad performance, because MySQL can't use an index for this kind of condition. It's sound advice of Marc to normalize your tables, be rid of this problem and gain performance by use of an index.
FIND_IN_SET() is used for this kind of thing, but you'll need to strip out the spaces too, since those shouldn't be in there. Here, I used REPLACE() to remove the spaces:
SELECT * FROM example
WHERE FIND_IN_SET('12', REPLACE(participants, ' ', ''))
I thought I had a very simple query to perform, but I can't seem to make it work.
I have this table with 2 columns:
version_id trim_id
1 15
1 25
1 28
1 30
1 35
2 12
2 25
2 33
2 48
3 11
3 25
3 30
3 32
I am trying to get any version-id's that have say a sub-set of trim_id's. Let's say all version_id's that have trim_id's 25 and 30. My obvious attempt was :
SELECT * FROM table WHERE trim_id=25 AND trim_id=30
I was expecting to have version_id 1 and 3 as a result, but instead I get nothing.
I am working with the latest version of MAMP, which has some odd behavior, like in this case it just tells me its 'LOADING' and never gives me an error message or something. But that's normally the case when there is no data to return.
This is InnoDB, if that helps.
Thanks for your input.
Your query does not work because you are using AND and the trim_id cannot have two different values at the same time, so you need to apply Relational Division to get the result.
You will need to use something similar to the following:
SELECT version_id
FROM yourtable
WHERE trim_id in (25, 30)
group by version_id
having count(distinct trim_id) = 2
See SQL Fiddle with Demo.
This will return the version_id values that have both 25 and 30. Then if you wanted to include additional columns in the final result, you can expand the query to:
select t1.version_id, t1.trim_id
from yourtable t1
where exists (SELECT t2.version_id
FROM yourtable t2
WHERE t2.trim_id in (25, 30)
and t1.version_id = t2.version_id
group by t2.version_id
having count(distinct t2.trim_id) = 2);
See SQL Fiddle with Demo
SELECT *
FROM table
WHERE trim_id IN(25,30)
This is a follow on from another question i made
I have this query that counts all the records up to a certain point i choose whcih works fine
SELECT count(*)FROM news WHERE id < 18
this query gives me a count of 7
I am now having problems with the offset which would be the result of the above query
I tried using this query
SELECT * FROM `news` ORDER BY id DESC LIMIT 7,1
but i get id number 13 instead of 18
The ids i should have is 2, 7, 10, 11, 12, 13, 16, 18, 19, 20, 21, 22, 23
I have tried using order by id desc in the count query which does give a different result but still wrong id displayed
I dont see a problem here: You order the result by id DESC which means your result is ordered by other way around and 8th value(0..7) is 13.
Try sorting it by ASC then it will give you 18
Is there a way I can return a set of mySQL rows in the order they're called. For instance, when I call this:
SELECT * FROM heroes WHERE id IN ( 41 , 48 , 38 , 14 , 47 , 44 ) LIMIT 6
I'd like the rows returned in that order. Is there a php function I could apply to the results afterward to achieve this ordering?
Yes, take a look at "Ordering by the order of values in a SQL IN() clause" answers.
The solution is very well described there.
You could try ORDER BY FIELD(id, 41, 48, 38, 14, 47, 44) ASC.
EDIT: Found a better function than FIND_IN_SET.