I have table in MySQL database where one column type is longtext and there are stored numbers. I need to get content from table sorted by numbers in that column.
SELECT * FROM wp_postmeta WHERE meta_key = 'rating_avg' ORDER BY meta_value
With this query sorting is not proper and looks like:
0
1.6
10
5
but I need like this:
10
5
1.6
0
I may not change column type, because this column have many different types of data. Is there any possibility to change column type temporary in SQL query?
What you are looking for is CAST.
CAST(expr AS type)
Your SQL Query should look like this:
SELECT * FROM wp_postmeta WHERE meta_key = 'rating_avg' ORDER BY CAST(`meta_value` AS DECIMAL) DESC
This can also be accomplished through native WordPress functions without having to resort to raw SQL. Give WP_Query an orderby parameter of meta_value_num rather than meta_value (reference).
It's been over a year since the question was asked, but for reference to anyone that came here after the search, the answer is
CAST(meta_value AS DECIMAL(10,2))
so the query should be:
SELECT * FROM wp_postmeta WHERE meta_key = 'rating_avg' ORDER BY CAST(`meta_value` AS DECIMAL(10,2)) DESC
Related
I've read multiple posts and find nothing wrong with my syntax, can someone point out the error?
I am testing some queries in PHP MyAdmin, on a WordPress Database. The table I am querying has a meta_key of "Listing-End-Date" and meta_values of "2018/06/30".
My query looks like this:
SELECT * FROM `table`
WHERE `meta_key` LIKE 'Listing-End-Date'
AND STR_TO_DATE('meta_value', '%Y/%m/%d') > CURDATE()
and it returns 0 results. To test my STR_TO_DATE format, I did a new query:
SELECT `meta_key` , STR_TO_DATE( 'meta_value', '%Y/%m/%d' )
FROM 'table'
WHERE `meta_key` LIKE 'Listing-End-Date'
I get the expected 1 result returned, but the date is NULL. Is it because I am using PHP My Admin or did I type something wrong?
Meta_value should be ticked and not quoted inside string_to_date
You were trying to convert string value 'meta_value' to date which obviously fails. You probably wanted to reference a column meta_value from table meta_key instead:
SELECT * FROM `table`
WHERE meta_key LIKE 'Listing-End-Date'
AND STR_TO_DATE(meta_value, '%Y/%m/%d') > CURDATE()
I've removed backticks as they're not required here (all but table). Though, you really shouldn't name your table with a reserved keyword even if you can using backticks.
Also, one thing to note would be that meta_key LIKE 'Listening-End-Date' is equivalent of equality comparison meta_key = 'Listening-End-Date' because you don't do partial search (you're not using % anywhere).
Try to use DATE_FORMAT()
check this page
I want to round the numbers to 2 decimal points in a woo commerce database table wp_postmeta.
There is a column 'meta_key' with row '_price' and a column 'meta_value' which has all kinds of data in it as well as the numbers in the row '_price' that need to be rounded and reduced to two decimal places.
(I know I'll also have to do that for _regular_price)
I am a complete beginner with SQL. I've managed to do a few things in PHPMyAdmin by using the Search to give me the syntax or copying and changing answers found on the net, but this one has me stumped, just not enough knowledge yet.
I managed to add 10% to all the prices with this query -
update wp_postmeta set meta_value = meta_value * 1.10 where meta_key='_regular_price'
I've read that there's a 'ROUND' function, but not sure how to write the query.
I'm guessing it would be something like this -
UPDATE wp_postmeta ROUND meta_value where meta_key='_price'
Hope someone here can help.
The documentation on round says:
ROUND(X), ROUND(X,D)
Rounds the argument X to D decimal places. [...] D defaults to 0 if not specified.
So your update would be:
UPDATE wp_postmeta
SET meta_value = ROUND(meta_value, 2)
WHERE meta_key='_price'
Display formats
If your concern is to display a number with 2 decimal digits, it might be better to keep the complete precision as it is, but change the way you select values from your table, and us format:
Without the above update, you can still do this:
SELECT FORMAT(meta_value, 2)
FROM wp_postmeta
WHERE meta_key='_price'
If in your table you have the value 15.002916 then the above select will render it to a string: 15.00.
Finally, if the data type of meta_value is a varchar (so, not a numerical data type), you can of course store the additional trailing zeroes:
UPDATE wp_postmeta
SET meta_value = FORMAT(meta_value, 2)
WHERE meta_key='_price'
But do realise that this only works as expected if the data type is of the text kind. In numerical data types the values 15.00 and 15 are exactly the same; it is just their display format which is different.
You can round your column with Round Function ,
Use function like this :
SELECT ROUND(column_name,decimals) FROM table_name;
in this case write query like below :
select Round(_Price , 2) from your table name
I want to add Multiple AND conditions in SQL query as Follows...
SELECT CONSUMER_NUMBER
FROM CONSUMER_INFO
WHERE YEAR=2014
AND CONT='USA'
AND ROWNUM=1;
But it does not work...How can I achieve this?
I assume YEAR is an Integer number, best practice would be to have a column type as data, and get the year using the YEAR function in SQL.
SELECT CONSUMER_NUMBER
FROM CONSUMER_INFO
WHERE YEAR(Data) =2014
AND CONT = 'USA'
AND ROWNUM = 1 ;
Something like this, but anyway I don't think this is the problem with your query, can you prinscreen the error?
PS : Also if your YEAR column is varchar or anything like that don't forget you have to use '' ;
This and conditions are right. Do you have column ROWNUM on your table. And what is datatype of YEAR column. You can show me your query errors?
I have the following MySQL table fields:
description1, description2, description3: Varchar(500)
value: int
and wish to find the records where at least one of the description includes the string searched by the user.
Right now I am using the following query. It works, but it takes about 1.5 second to return the results.
SELECT `table`.`value`,
`table`.`description1`,
`table`.`description2`,
`table`.`description3`
FROM `table`
WHERE ( `table`.`description1` LIKE '%string%'
OR `table`.`description2` LIKE '%string%'
OR `table`.`description3` LIKE '%string%' )
ORDER BY `table`.`value` DESC LIMIT 0 , 9
Is there any way to get the results faster?
(Note that the value field is already indexed).
add full text index and instead like use AGAINST
I am trying to build a MySQL query that has an order by statement. This is what I am trying to do:
SELECT *
FROM tbl_product
ORDER BY retail_price ONLY IF wholesale_price IS NULL OTHERWISE ORDER BY wholesale_price.
I have no idea where to start. I found an article that uses ORDER BY COALESCE but I also found that this could have performance issues.
Any advice is appreciated.
SELECT *
FROM tbl_product
ORDER BY ifnull(wholesale_price, retail_price);
Note that you don't need to select the value you're ordering by - it can be any expression