omit results starting with certain character - mysql

I am trying to retrieve a list of names from a table where the surname does not start with z or Z using mysql. I tried combining substring with instr to accomplish this. Attempt below:
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE SUBSTRING(
CONCAT(FName, ' ' ,SName),
INSTR(CONCAT(FName, ' ' ,SName), ' ') +1,
1)
<> 'z'
OR SUBSTRING(
CONCAT(FName, ' ' ,SName),
INSTR(CONCAT(FName, ' ' ,SName), ' ') +1,
1)
<> 'Z'
ORDER BY SName
My attempt is returning results with z as the first letter of the surname. Can anyone explain why? Or if there is a better way to achieve this

This can be much shortened with LIKE:
SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE FName NOT LIKE 'z%' AND FName NOT LIKE 'Z%';
IIRC LIKE is case-sensitive since MySQL v5.6.x
I wouldn't write it like
...WHERE LOWER(FName) NOT LIKE 'z%';
since applying functions on columns prevent MySQL from using the index on the column (if one exists).

SELECT DISTINCT SQL_CALC_FOUND_ROWS CONCAT(FName," ",SName)
FROM Names
WHERE FName REGEXP '^[^z]';

Related

Search 2 columns for parts of string in MYSQL

I would like to search inside 2 columns for parts of a string. It's a backwards way of searching so I doubt it's possible.
I think part of it would have something to do with:
REPLACE( event_name, ' ', '')
and
REPLACE( venue_name, ' ', '')
to get rid of the space.
I also thought REGEX might come into it.
Absolutely no idea where to start! PSUEDOCODE might be:
CONCAT(
event_name = REPLACE( part of :search, ' ', '')
,
venue_name = REPLACE( part of :search, ' ', '')
)
= :search
If I used noddybobstheatre as the search term I want to search the column event_name for part of it and venue_name for another part and when the 2 parts are put together they equal the search term.
event_name = 'noddy'
venue_name = 'bobs theatre'
Like I said, this might be a crazy ask...
Reading what you need I thought that the like statement should use the column and not the search value.
here is my reproduction of what I understood from your problem
CREATE TABLE movies
(`word1` varchar(5), `word2` varchar(7))
;
INSERT INTO movies
(`word1`, `word2`)
VALUES
('micko', 'to'),
('he', 'llbrane'),
('mick', 'oto'),
('hell', 'brane')
;
then here is the sql statement that I used
select * from movies where 'mickoto' like CONCAT(Replace(word1,' ',''),'%') and 'mickoto' like CONCAT('%',Replace(word2,' ',''))
all you have is to adapt this to your context
here is the fiddle execution
use this,
concat(replace(event_name,' ',''),replace(venue_name,' ',''), )=:search

How do I select a particular string length starting from a specific sign(such as ':') in mysql?

For e.g. If I have column entry as
'The only verdict is : Vendetta'
and the same column has another entry as
'I believe in : Harvey Dent'
and all I want to select via my query is 'Vendetta' and 'Harvey Dent' i.e. the string just after the : sign, how do I do it?
Can it be restricted to select upto a specific number of characters after the sign?
If you only have one : in your string, you can give a look at SUBSTRING_INDEX:
SELECT
col,
SUBSTRING_INDEX(col, ':', -1)
FROM
tablename
or you can use SUBSTRING with LOCATE:
SELECT
col,
SUBSTRING(col FROM locate(':', col)+1)
FROM
tablename
WHERE
col LIKE '%:%'
(in your example you probably need to substitute ':' with ' : ' and +1 with +3)

Use replace with in clause

Got ids stored in DB with Json format like this
'["1454","474","545"]'
I can build list IDs :
SELECT replace
(replace(
replace(
replace('["1454","474","545"]','[','\'')
,']','\'')
,'"','')
,',','\',\'')
mySql returns '1454','474','545'
But when I try to list DB records from this build list of IDs :
SELECT col FROM table WHERE col in (REPLACE
(REPLACE(
REPLACE(
REPLACE('["1454","474","545"]','[','\'')
,']','\'')
,'"','')
,',','\',\''));
mySql says "0 records" even if I add a "SELECT" before the first "REPLACE"
Any help ?
Try below query, you need to add select in your in clause also:
SELECT col FROM table WHERE col in (select REPLACE
(REPLACE(
REPLACE(
REPLACE('["1454","474","545"]','[','\'')
,']','\'')
,'"','')
,',','\',\''));
Alas, you cannot use in with a comma delimited string. It takes a list of elements, but not within a string. So, this works as you expect:
where x in (1,2,3)
This does not work as you expect (although it does work as I expect0;
where x in ('1,2,3')
This looks for one value of x that is the string '1,2,3'.
The solution is to use the MySQL function find_in_set():
SELECT col
FROM table
WHERE find_in_set(col, REPLACE(REPLACE(REPLACE('["1454","474","545"]', '[','\''
), ']', '\''
), '"', ''
), ',', '\',\''
);
To be honest, though, you might be better off with something like:
where '["1454","474","545"]' like concat('%;', col, '&%')

mysql query where second words starts with a letter

I have a table with:
Name Surname and Outputname ( = Name Surname )
Name and Surname should always have a value, but some time are empty.
I need to select from table by surname and i can't use surname field.
I can't even change field values with a script because i get this table from an outside source and can be change any time.
Database is MySQL 4.x
Can i select by second word in Outputname starting with some letter?
something like
SELECT Outputname FROM USERS
WHERE seconword(Outputname) LIKE 'A%' SORT BY seconword(Outputname) ASC
try this
SELECT
SUBSTRING_INDEX(Outputname, ' ', -1) as SecondWord
FROM USERS
WHERE SUBSTRING_INDEX(Outputname, ' ', -1) LIKE 'A%'
ORDER BY SecondWord ASC
demo
One possible approach:
SELECT Surname
FROM (
SELECT SUBSTRING_INDEX(Outputname, ' ', -1)
AS Surname
FROM Users) AS S
WHERE Surname LIKE 'A%'
ORDER BY Surname;
SQL Fiddle. This method is based on assumption that Outputname's format is always 'FirstName LastName' (i.e., ' ' symbol is used as a delimiter, and used only once each time).
I didn't understand your question at all. So you only have access to Outputname (composed by two words name+surname) and you have to sort it by the second word right?
Try something like (it worked for me):
SELECT
Outputname,
SUBSTRING_INDEX(Outputname, ' ', -1) as SecondWord
FROM USERS
ORDER BY SecondWord ASC
Clause SUBSTRING_INDEX(Outputname, ' ', -1) as SecondWord returns the last word placed after a space. So If you have Outputname = 'Maria Callas' it returns 'Callas', if you have Outputname = 'Sophia Cecilia Kalos' it returns 'Kalos'.

mysql: instr specify word boundaries

i want to check if a string contains a field value as a substring or not.
select * from mytable where instr("mystring", column_name);
but this does not search on word boundaries.
select * from mytable where instr("mystring", concat('[[:<:]]',column_name,'[[:>:]]');
does not work either. how to correct this?
You can do this using the REGEXP operator:
SELECT * FROM mytable WHERE 'mystring' REGEXP CONCAT('[[:<:]]', column_name, '[[:>:]]');
Note, however, that this is slow. You might be best off using the MySQL's FULLTEXT search feature if you care about words. Or do a normal InStr() check then filter the results.
If you don't need the return value of the instr use like instead
select * from mytable where column_name like '%mystring%';
As already discussed in the question you asked yesterday, no indexes can be used and performance is going to be bad, but this could work:
select *
from mytable
where 'mystring' = column_name -- exact match
or 'mystring' like concat('% ', column_name) -- word at the end
or 'mystring' like concat(column_name, ' %') -- word at beginning
or 'mystring' like concat('% ', column_name, ' %') -- word in the middle