I have a column abc in table t1. The column has values
A1
A2
A3
A4
.
.
A12
B1
B2
.
.
.
B12
C1
C2
.
.
.
C12
H1
.
.
H12
I wanna sort them such that the output is
A1
B1
C1
.
.
H1
A2
B2
C2
.
.
.
H2
.
.
.
.
A12
.
.
.
H12
A select * from abc statement gives A1,A10,A2.... as output. I am trying to use SUBSTR but haven't gotten it right.
This should do it;
SELECT * FROM TEST
ORDER BY SUBSTRING(VALUE, 2) + 0,
SUBSTRING(VALUE, 1, 1);
Demo here.
Considering the format is always a single alpha char followed by any number of digits:
ORDER BY RIGHT(colname, LENGTH(colname)-1)
maybe this:
ORDER BY LEFT(colname,1), RIGHT(colname, LENGTH(colname)-1)
order by lpad(date_created,1,0) ASC
#bfavaretto i think if you use -1 in length colname, and you have 3 characters like in example you will have some strange results.
SELECT Square
FROM Table1
ORDER BY
CASE WHEN Square REGEXP '^[A-Z]{2}'
THEN 1
ELSE 0
END ASC,
CASE WHEN Square REGEXP '^[A-Z]{2}'
THEN LEFT(Square, 2)
ELSE LEFT(Square, 1)
END ASC,
CASE WHEN Square REGEXP '^[A-Z]{2}'
THEN CAST(RIGHT(Square, LENGTH(Square) - 2) AS SIGNED)
ELSE CAST(RIGHT(Square, LENGTH(Square) - 1) AS SIGNED)
END ASC
Related
How to write a query to create a conditional and calculated field in SQL?
I am trying to merge two or more entities and the context of the entities are similar to the sample ones below. I am having an issue with regards to how to create the calculated field with the column F and £ of C-A. I would greatly appreciate if I could get some help.
FlightTable AirportTable
AKEY CODE F £ Code City Country
001 LHR C 10 ATL Atlanta USA
002 BOS C 15 BOS Boston USA
003 BOS A 9 . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .
101 MAN C 21 VIE Schwechat Austria
102 VIE A 9 ZRH Kloten Switzerland
I am trying to get a result of a joined entity similar to:
Joined table
CODE CITY COUNTRY (£ Calculated Field)
001 BOS Boston USA 4
. . . . . . . . . . . . . .
You may be able to use a case statement in the calculation.
SELECT AIR.Code, AIR.City, AIR.Country, SUM(CASE WHEN flight.f = 'C' then £
when flight.f = 'A' then (-1) * £
else null
end) as "£ calculated"
from AirportTable as "air"
join FlightTable as "flight" on "air".CODE = "flight".code -- could be AKEY = AKEY
I think you just want two joins:
select a.*, (fc.£ - fa.£) as calculation
from airporttable a join
flighttable fc
on fc.code = a.code and fc.f = 'C' join
flighttable fa
on fa.code = a.code and fa.f = 'A';
I want to find every word frequency in a column by using MySQL only (if possible). For example:
Table:
id message
1 I want to eat pizza
2 I wanted chocolates
3 He doesn't like me
Query: ???
Result:
Word Frequency
I 2
want 1
to 1
eat 1
pizza 1
wanted 1
etc..
Is it possible? If so please help, thank you
You need to split the data. This is a pain:
select substring_index(substring_index(message, ' ', n.n), ' ', -1) as word,
count(*)
from (select 1 as n union all select 2 union all select 3 union all
select 4 union all select 5
) n join
t
on n.n <= 1 + length(message) - length(replace(message, ' ', ''))
group by word;
The above assumes that all messages are five words or less. You can extend the number in the first subquery for longer messages.
Here is a php example. You will probably have to tweak it a bit.
lets assume you have a word_frequency table with a unique column word and an integer for count. Also, this is susceptible to SQL injection, so you should be careful. But this should get you started.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysqli_query($con,"SELECT message FROM table1");
while($row = $results->fetch_assoc()) {
$words = explode(" ", $row['message']);
foreach ($words as $word) {
mysqli_query($con,"INSERT INTO word_frequency (`word`,`count`) VALUES ('$word',1) ON DUPLICATE KEY UPDATE `count`=`count`+1;");
}
}
mysqli_close($con);
I wish to extract only a number if it's between 8 and 12 digits long, otherwise I wish it to be a blank. However in the column of data there can be text which I want as a blank.
Have tried may alterations of the code below with different brackets, though I get an error
SELECT CASE WHEN
isnumeric(dbo.worksheet_pvt.MPRNExpected) = 0 THEN '' ELSE(
CASE WHEN(
len(dbo.worksheet_pvt.MPRNExpected) >= 8
AND len(dbo.worksheet_pvt.MPRNExpected) < 13
) THEN dbo.worksheet_pvt.MPRNExpected ELSE ''
END
) AS [ MPRN Expected ]
Assuming you are using SQL Server, I would suggest:
select (case when p.MPRNExpected not like '%[^0-9]%' and
len(p.MPRNExpected) between 8 and 12
then p.MPRNExpected
end) as MPRN_Expected
. . .
from dbo.worksheet_pvt p
Presumably, you don't want isnumeric(), because it allows characters such as '.', '-', and 'e' in the "number".
The problem with your code is that you have two case expressions and they are not terminated correctly.
As a note, in MySQL, you would use regular expressions:
select (case when p.MPRNExpected regexp '^[0-9]{8-12}$'
then p.MPRNExpected
end) as MPRN_Expected
. . .
from dbo.worksheet_pvt p
Try this query
SELECT CASE WHEN ISNUMERIC(COLUMN_NAME)=0 THEN '' ELSE
CASE WHEN (LEN(COLUMN_NAME)>=8 AND LEN(COLUMN_NAME)<13)
THEN COLUMN_NAME ELSE ''END END AS data FROM TABLE_NAME
Thanks.
I am trying to get letters before numbers with a descending order and am finding it very difficult to do so.
I have tried using Case ordering
ORDER BY CASE WHEN ' . $this->sortbycast . ' LIKE \'%[a-z]%\' THEN 0 WHEN ' . $this->sortbycast . ' LIKE \'%[0-9]%\' THEN 1 ELSE ' . $this->sortbycast . ' END DESC
Regular expression as follows
' ORDER BY ' . $this->sortbycast . ' REGEXP \'^[a-z]\', ' . $this->sortbycast . ' desc';
and CAST()
' ORDER BY CAST(' . $this->sortbycast . ' AS UNSIGNED), ' . $this->sortbycast;
But none of these have had the desired result. All work correctly with ascending order. Any help would be appreciated. Thanks.
Update due to Strawberry's response
DB Fiddle here
My present query looks like this - reformatted for legibility:
SELECT p.*
, m.data
, m.id author_id
FROM anchor_posts p
JOIN anchor_post_meta m
ON m.post = p.id
WHERE p.category = 5
AND m.extend = 3
ORDER
BY CAST('anchor_post_meta.data' AS UNSIGNED) -- NOTE: THIS IS A STRING !?!
, m.data DESC
LIMIT 12;
The desired result
The desired result would be to have items show in the following order:
data
==============
A Title
B Title
C Title
D Title
70000
60000
45000
30000
25000
12000
Please see the following version of the DB Fiddle to see more clearly.
I've got 5 columns in table:
- id / a /b /operation/ c
where for example: 1 / 2 /3 / + / 5
What is my goal is to check if operation (+/-/*/%) is correct with the formula (a (operation) b = c) and show only those records where formula is true.
Honestly I've been trying to solve that but unfortunately I really don't know how to...
For sure I have to start with operation column:
(IF(operation = '+', a+b, IF( operation = '-', a-b, IF(operation = '*', a*b, IF(operation = '/',a/b, ''))))
Shall I mix it with IF CASE maybe? What's more, zero exepction needs to be added
Use case:
select (c = (case when operation = '+' then a + b
when operation = '-' then a - b
when operation = '/' then a / nullif(b, 0)
. . .
end)
) as expected_equals_actual