Special sorting for MySQL result - mysql

Is it possible to get MySQL to put a row which matches some condition on the first position?
For instance, say that my result is [1, 3, 5, 6, 7, 1987] and I want it to become [6, 1, 3, 5, 7, 1987] (I want the number 6 to be on the first position)

I feel you are searching for this
SELECT * FROM `pctable`order by colName=6 desc, colName
so colName = 6 will be in top and then for others it will be id wise sort.

Try to use CASE expression in your Order by clause. Such as
ORDER BY CASE WHEN COL_VALUE = 'xxx' THEN '1'
WHEN COL_VALUE = 'xxx' THEN '2'
ELSE COL_VALUE END ASC;

Or with IF in the SELECT somehow:
SELECT IF(number = 6, number, ''), number FROM colname

Related

Add hyphen in a string after every 2 characters using in mysql select statement

I have a mysql table with some data. In that i have a column(mobile_number) and that column value is like mobile number (ex:1234587920).
my expected out put is : 12-34-58-79-20
I want to add hyphen after every two numbers.
use concat() for joining strings and use substr() for split a string in mysql :
split (column,start,count) select column from start position for count charachter
concat(column1,"-",column2) will be column1-column2
SELECT CONCAT(SUBSTR(phone,1,2),'-',SUBSTR(phone,3,2),'-',SUBSTR(phone,5,2),'-
',SUBSTR(phone,7,2),'-',SUBSTR(phone,9,2)) FROM `table`
I would definitely use concat_ws() for this:
select concat_ws('-',
substr(mobile_number, 1, 2),
substr(mobile_number, 3, 2),
substr(mobile_number, 5, 2),
substr(mobile_number, 7, 2)
)
Here is a db<>fiddle.
this will work:
SELECT concat(SUBSTRING("1234587920", 1, 2),"-",SUBSTRING("1234587920",3,2),"-
",SUBSTRING("1234587920", 5, 2),"-",
SUBSTRING("1234587920", 7, 2),"-",
SUBSTRING("1234587920", 9, 2)) AS ExtractString;
for your table query is like :
SELECT concat(SUBSTRING(mobile_number, 1, 2),"-
",SUBSTRING(mobile_number,3,2),"-",SUBSTRING(mobile_number, 5, 2),"-",
SUBSTRING(mobile_number, 7, 2),"-",
SUBSTRING(mobile_number, 9, 2)) ExtractString from tablename;
you can use substr and contact_ws command in mysql
update tmp_pay SET date_fix = concat_ws('-',substr(date_at,1,4),substr(date_at,5,6),'01') ;
you can try this
select concat( right(phone_number,3) , "-" , mid(phone_number,4,3) , "-", right(phone_number,4)) from table

SQL how to add a leading 0

Any help would be greatly appreciated.. Im quite new to mySQL and figured there has to be an easier way to update my database.
So my issue is that I have a number of records that don't have a leading zero but I need the total number of characters in any record to be 5.
e.g. 6930 should in fact be 06930
I came across this code but I am unable to get it to work correctly, could anyone point me in the right direction.
UPDATE `it_asset_register`.`tab_id_master`
SET tab_id_master.ID = LEFT('00000', 5-LEN(tab_id_master.ID)) + tab_id_master.ID
WHERE ID = '8407' AND LEN(tab_id_master.ID)<5 AND Len(tab_id_master.ID)>0;
Thanks
LPAD should do the job here:
SELECT LPAD ('123', 5, '0');
00123
SELECT LPAD ('12345', 5, '0');
12345
Try:
UPDATE `it_asset_register`.`tab_id_master`
SET tab_id_master.ID = LPAD (tab_id_master.ID, 5, '0')
WHERE ID = '8407' AND LENGTH(tab_id_master.ID)<5 AND LENGTH(tab_id_master.ID)>0;
To concatenate two or more strings in MySQL, you must use the CONCAT() function.
A possible solution can be:
UPDATE tab_id_master
SET id = SUBSTRING(CONCAT('00000', id), -5)
WHERE LENGTH(id) > 0 AND LENGTH(id) < 5;
Alternatively you can use the LPAD() function:
UPDATE tab_id_master
SET id = LPAD(id, 5, '0')
WHERE LENGTH(id) > 0 AND LENGTH(id) < 5;
Regards
UPDATE
table_name
SET
column_name = RIGHT('00000' + CAST(column_name AS CHAR), 5)
This concatenates the 5 zeros and your ID to something like '000001234'. Then it selects the 5 rightmost characters.
You didn't specify tab_id_master.ID type. If it is of a numeric type you wouldn't be able to store a number with leading zeros but otherwise you could use LPAD():
SET tab_id_master.ID = LPAD(tab_id_master.ID, 5, 0)

MySQL ORDER BY letters then numbers

I have MySQL Query with an ORDER BY that looks like this:
ORDER BY home_status = 2, home_lot_size, home_status ASC, home_price
Now the home_lot_size can either be Townhome, 30, 36, 40, 46, 50 but the order comes out like this, 30, 36, 40, 46, 50 and then Townhome, I am looking to get Townhome at the top then 30, 36, 40, 46, 50
is this possible?
You can use a CASE clause to create a new column you can sort on. Something like this
SELECT
CASE home_lot_size
WHEN 'Townhome' THEN '00'
ELSE home_lot_size
END as sort_by
FROM table
ORDER BY sort_by
Here is a simple solution,
SELECT * FROM your_table_name WHERE (home_lot_size='Townhome' OR 1=1) ORDER BY home_lot_size='Townhome' DESC
You don't need to use case for this.
You can use a CASEin the ORDER BY:
ORDER BY home_status = 2,
case when home_lot_size = 'Townhome' then '0' else home_lot_size end,
home_status ASC, home_price
Yes, it's possible. One quick fix would be to have home_lot_size evaluated in a numeric context, such that the value of Townhome would be evaluated as zero, and the other values as integers. An easy way to do that is to add zero, e.g.
ORDER BY home_status = 2, home_lot_size + 0, home_status, home_price
^^^
In the more general case, we could use a more complex series of expressions, e.g.
ORDER BY home_status = 2
, IF(home_lot_size='Townhome',0,1)
, home_lot_size
, home_status
, home_price

Mysql - Calculate soundex difference between two strings

I have some song names and their pre-calculated soundex stored in a mysql table. I want to compare the soundex of user input with the pre-calculated soundex'es. And get the results in ascending order of difference between the user input and song name.
I have tried the following query (in java):
String query="SELECT * FROM song ORDER BY STRCMP(pre_calculated_soundex,SOUNDEX("+user_input+")) ASC ";
But strcmp only returns 1,0 or -1. So ordering is not correct.
Also tried WHERE pre_calculated_soundex=SOUNDEX(user_input), but this just returns exactly matching soundex.
Completely low-tech and assuming that only first four characters of soundex function is being used and also assuming that "aaaa" is the user input
SELECT *
FROM song
ORDER BY Substr(pre_calculated_soundex, 1, 1) =
Substr(Soundex("aaaa"), 1, 1)
+ Substr(pre_calculated_soundex
, 2, 1) =
Substr
(Soundex("aaaa"), 2, 1)
+ Substr(pre_calculated_soundex, 3, 1)
= Substr(Soundex("aaaa"), 3, 1)
+ Substr(pre_calculated_soundex, 4, 1
)
= Substr(Soundex("aaaa"), 4, 1)

MySQL to PostgreSQL query rewrite using "IN"?

I'm transferring over some queries from MySQL to PostgreSQL and I'm stumped on how to rewrite the following query to work in PostgreSQL:
SUM(phoneid IN (1, 2, 6, 8)) AS completedcalls
I originally thought I could just do SUM(SELECT phoneid FROM myTable WHERE phoneid = 1 OR phoneid = 2 etc etc, but I do not believe you can have a SELECT within a sum.
I also tried using a WITH query but had no luck getting that to work.
how about using CASE
SUM(CASE WHEN phoneid IN (1, 2, 6, 8) THEN 1 ELSE 0 END)
count(phoneid in (1,2,6,8) or null)
bool may be cast to integer:
SUM(CAST(phoneid IN (1, 2, 6, 8) AS INTEGER)) AS completedcalls