I want to retrieve records from table, all the records are decimal values in (width x Height) format
using like clause I want to to get data but query didn't work for it
Table:
id widthheight
1 17.14X12.41
2 23.4X39.8
select * from test where widthHeight like '%17X12%'
As said above, create two columns 'width' and 'heigth'
select * from (
select
id,
SUBSTRING_INDEX(widthHeight, 'X', 1) width,
SUBSTRING_INDEX(widthHeight, 'X', -1) height
from mytable) x
where floor(x.width)=17 and floor(x.height)=12
Your original query was not working because you should have done this:
select *
from mytable
where widthHeight like '%17%' and widthHeight like '%12%'
Better solution is to use a regex query:
SELECT * FROM test WHERE widthHeight REGEXP '17(\.\d+)?X12(\.\d+)?'
This regexp will match with following values:
17X12
17.14X12.41
17.00X12.00
17.9999999X12.999999
You can playground with this regex on the awesome Regex101. Go to this link.
Note: This query won't be efficient. Consider splitting your data into two columns named height and width that uses DECIMAL column type so you can do more efficient queries.
You should ideally just store the width and height as two separate decimal columns. That being said, you might be able to work with what you have now using some string tricks:
SELECT *
FROM test
WHERE
SUBSTRING_INDEX(widthHeight, 'X', 1) REGEXP '17[[:>:]]' AND
SUBSTRING_INDEX(widthHeight, 'X', -1) REGEXP '12[[:>:]]';
Related
I have a varchar(30) column that looks like this:
953-41
975-12
952-13
934-34
All numbers of the column share the structure of: 3 numbers and a dash followed by more numbers.
I want to make a query that works like SELECT * FROM tablename WHERE value = 95341
And get '953-41' only using numbers in the WHERE clause.
I can't change my database and remove the dash, I need to search with a numeric value on rows that mix the numbers I want with a dash in between.
you can try:
MYSQL:
SELECT * FROM tablename WHERE value = INSERT(95341,4,0,'-')
SQL Server:
SELECT * FROM tablename WHERE value = STUFF(95341,4,0,'-')
You can use this:
SELECT *
FROM yourTable
WHERE CAST(REPLACE(colName, '-', '') AS UNSIGNED) = 95341
I am looking to compare the results of 2 cells in the same row. the way the data is structured is essentially this:
Col_A: table,row,cell
Col_B: row
What I want to do is compare when Col_A 'row' is the same as Col_B 'row'
SELECT COUNT(*) FROM MyTable WHERE Col_A CONTAINS Col_B;
sample data:
Col_A: a=befd-47a8021a6522,b=7750195008,c=prof
Col_B: b=7750195008
Col_A: a=bokl-e5ac10085202,b=4478542348,c=pedf
Col_B: b=7750195008
I am looking to return the number of times the comparison between Col_A 'b' and Col_B 'b' is true.
This does what I was looking for:
SELECT COUNT(*) FROM MyTable WHERE Col_A LIKE CONCAT('%',Col_B,'%');
I see You answered Your own question.
SELECT COUNT(*) FROM MyTable WHERE Col_A LIKE CONCAT('%',Col_B,'%');
is good from performance perspective. While normalization is very good idea, it would not improve speed much in this particular case. We must simply scan all strings from table. Question is, if the query is always correct. It accepts for example
Col_A: a=befd-47a8021a6522,ab=7750195008,c=prof
Col_B: b=7750195008
or
Col_A: a=befd-47a8021a6522,b=775019500877777777,c=prof
Col_B: b=7750195008
this may be a problem depending on the data format. Solution is quite simple
SELECT COUNT(*) FROM MyTable WHERE CONCAT(',',Col_A,',') LIKE CONCAT('%,',Col_B,',%');
But this is not the end. String in LIKE is interpreted and if You can have things like % in You data You have a problem. This should work on mysql:
SELECT COUNT(*) FROM MyTable WHERE LOCATE(CONCAT(',',Col_B,','), CONCAT(',',Col_A,','))>0;
SELECT * FROM MyTable WHERE Col_A = Col_B (AND Col_A = 'cell')
^^ Maybe you are looking for this statement. The part in brackets is optional.
If this is not the solution, please supply us with further information.
The easiest way would be to use the IN operator.
SELECT COUNT(*) FROM MyTable WHERE Col_A IN (Col_B);
More info on the IN operator: http://www.w3schools.com/sql/sql_in.asp
There's also the SUBSTRING() or MID() (depending on what you're using) function if you know that the substring will be in the same position everytime.
MID()/SUBSTRING() function: http://www.w3schools.com/sql/sql_func_mid.asp
You can use SUBSTRING_INDEX to extract a delimited field from a column.
SELECT COUNT(*)
FROM MyTable
WHERE Col_B = SUBSTRING_INDEX(SUBSTRING_INDEX(Col_A, ',', 2), ',', -1)
You need to call it twice to get a single field. The inner call gets the first two fields, the outer call gets the last field of that.
Note that this will be very slow if the table is large, because it's not possible to index substrings in MySQL. It would be much better if you normalized your schema so each field is in a separate column.
If column Col_a has data with format table,row,cell then search expression will be next:
SELECT COUNT(*) FROM MyTable AS MT
WHERE SUBSTRING(Col_A,
INSTR(Col_A, ',b=') + 3,
INSTR(Col_A, ',c=') - INSTR(Col_A, ',b=') + 3) = Col_B
I'm trying do something like this:
SELECT * FROM table WHERE column IN (1,2,3)
but in place of 1,2,3 I want to use a column from another table that contains a comma-delimited list just like "1,2,3" above.
I have tried to do this:
SELECT
GROUP_CONCAT(a.eating_area SEPARATOR ', ')
FROM table_areas a
WHERE a.eating_area_id IN (
SELECT
o.eating_area_ids
FROM table_offers o WHERE o.rid=1
)
however this only returns the value associated with 1, and not 2 or 3. Can this be done or is there another way to do this?
Many thanks
SELECT * FROM table t
WHERE IF(FIND_IN_SET(column,(SELECT "1,2,3" FROM otherTable WHERE 1))>=1,1,0)
-- FIND_IN_SET will return the position.
I don't know if it's the best way to do it but... i think it could work.
Source: Find_in_set
I need to write a query using MYSQL REGEXP that will find me rows that have a certain column begin with 11 or 12 or etc. I know I can use LIKE or LEFT(####,2) but would like to use the REGEXP option. My data is stored as 110001, 122122, 130013a and etc.
EDIT 1:
To make it more clear, I would like to express
SELECT * FROM table WHERE column LIKE '11%' or column LIKE '12%' or column LIKE '30%'"
with REGEXP
Thanks!
To match rows that start with any two digits you can use:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^[0-9][0-9]';
If you only want to allow rows starting with 11, 12 or 30 then you could use this:
SELECT *
FROM yourtable
WHERE yourcolumn REGEXP '^(11|12|30)';
This will not be able to use an index on the column so it may be slower than using LIKE.
I'd like to select rows from the database where the last character in the mov_id column equals to 1 and 2.
How would the query look like?
SELECT * FROM `myTable` WHERE `mov_id` LIKE '%1' OR `mov_id` LIKE '%2'
the % character is a wildcard which matches anything (like * in many other places)
If mov_id is a numeric value (TINYINT, INT, etc...) then you should use a numeric operator. For instance, use the modulo operator to keep the last digit
SELECT * FROM mytable
WHERE (mov_id MOD 10) IN (1, 2)
If mov_id is a string, you can use LIKE or SUBSTRING(). SUBSTRING() will be slightly faster.
SELECT * FROM mytable
WHERE SUBSTRING(mov_id, -1) IN ('1', '2')
If your table is big or that query is frequently run, you should definitely consider adding a column to your table, in which you would store mov_id's last digit/character, and index that column.
Try this way too:
SELECT field1
FROM table
WHERE RIGHT(field1, 1) = 'x'
it displays the fields that has last a value of x.
SELECT *
FROM Table
WHERE RIGHT(Column_name, 1) IN ('x')
if you want to match two character just replace 1 by 2.
In general:
RIGHT(COLUMN_NAME, NO_OF_CHARACTER_YOU WANT_TO_MATCH_FROM_LAST)
And if you want to match the starting char just use LEFT instead of RIGHT
You could also do something like:
select
your, fields, go, here
from table
where
substring(mov_id, (char_length(move_id) - 1)) = x
SELECT * FROM table WHERE mov_id REGEXP '1$' OR mov_id REGEXP '2$'