How to search using an sql statement? - mysql

How can I find all the ids of an SQL table? For example I want to search for the word "key" and return the ids in which this word was found.

Assuming that id is the name of a column in your table you would need to use LIKE
SELECT id
FROM YourTable
WHERE id LIKE '%key%'
The % is a wildcard meaning match any set of zero or more characters so this would find rows where the id value contains the substring (not necessarily word) "key".

SELECT [id field name] FROM [Tabel Name] WHERE [field to seach in for key] LIKE '%key%';

Would this be what you are looking for?
INSERT INTO dest_table (dest_id)
SELECT source_id
FROM source_table
WHERE source_column LIKE '%key%';

Related

Is "LIKE %%" identical to "LIKE %"?

Are there any differences between the following two queries:
SELECT name from Roster WHERE id LIKE %
and:
SELECT name from Roster WHERE id LIKE %%?
They are equivalent. The % wildcard matches zero or more characters. So both of these check that id is not NULL.
Note that the like pattern needs to be a string. So, both your example would generate syntax errors. You want:
where id like '%'
where id like '%%'
Also, if id is a number, then you should not be using a string operation on it -- under most circumstances.
Here you will get clarification :

Cannot check if a field contains also number

I'm trying to check if a field in a specific table contains also number, in particular I have a record that have the field name which contains this value: Besëlidhja Lezhë vs. Tërbuni Pukë 1 - 1, so I'm trying to get also all the rows of that table that contains a number inside the field name. I tried:
SELECT * FROM `venue` where `name` like '%[0-9]%'
but this will return an empty result, any idea?
This should tell you if name contains any digit (not tested)
SELECT * FROM venue WHERE name REGEXP '[0-9]'
You can try using a Regular Expression that filters for names in your name column with numeric characters . For example:
SELECT * FROM DATA WHERE name REGEXP '[a-z]...[0-9]';
mySQL allows you to use regular expression as a filter !
This should select out for names like Tërbuni Pukë 1 - 1. If you want to practice regular expressions this is a great website to test whether you have the right regex. https://regex101.com/
Hope this helps !
I believe this should work:
SELECT *
FROM venue
WHERE name like '%0%' or name like '%1%' or name like '%2%' or name like '%3%'
and so on til you get to 9. I hope this helps

how to know the count of words starting with a particular word in database?

I am using a mysql database for storing the data. The table consists of 2 columns s_no and name. I used index on this table on name. Now I want to get only words starting with a particular letter say "a" or "b" etc. How can I do it? Is there any SQL query for retrieving data in this fashion?
select s_no,name from tablename where name like 'a%' or name like 'b%'
select * from table_name
where name like 'a%'
select s_no,name from table where name like 'a%' or name like 'b%'
Select * from tablename where name like 'a%'
You can also pass the value inside parameters
#value varchar(50)
Select * from tablename where name like #value
so whatever characters u insert like a or b just mearge it with % and here you go
like #value=#value+ '%' before query .

Mysql statement to select names that start with specific range of characters

I'm writing the following statement to select names that starts with a range of characters.
select name from db.table where name like '[c-e]%';
The statement does not return anything to me. But when I change it to:
select name from db.table where name like 'c%';
It returns records.
What is the problem ?
instead of LIKE use REGEXP
select name from db.table where name REGEXP '[c-e].+';
See MySQL Pattern Matching
#juergen d is right but a little change required as you want to check all the name starting from in range [c-d] so
select name from db.table where name REGEXP '^[c-e].+';
"^" indicates that match when starting must match range [c-e]
As others have said, you could use REGEXP; but when you want a range of starting characters, you can also do:
SELECT name FROM db.table WHERE name >= 'c' AND name < 'f';
This could be faster, as it can take advantage of an index in the name field to avoid a full scan.
You should use REGEXP as:
SELECT name FROM db.table where name REGEXP '[c-e].+';

find elements of a varchar in another varchar

i have a varchar field with the content like these:
a,b,c,d
e,d,a,c
b,q,d,e
i need to do a query that select only the rows with the field that has elements equals with an input string.
ex
input: c,a
rows selected:
a,b,c,d
e,d,a,c
is possible without use the OR (field like '%a%' OR field like '%c%') ?
thanks
Yes, you can use...
WHERE
myField LIKE '%a%' AND myField LIKE '%c%'
However, this does sound like you've got a nasty field in your SQL.
Normally, you would use a link-table to specify this kind of information:
Table1
id
otherdata...
Table2
id
letter
LinkTable
Table1Id
Table2Id
So in LinkTable you would have many entries that link your records. So instead of storing 'a,b,c,d' in a field, you have four link records:
Table1Id Table2Id
1 1 (a)
1 2 (b)
1 3 (c)
1 4 (d)
Yes but you need a trick: Create a second column which contains the same value but sorted:
a,b,c,d
a,c,d,e
b,d,e,q
Now, you can query for %a%c%
This won't work if the query string can be a substring of any unwanted value. If this is the case, you need quotes:
"a","b","c","d"
"a","c","d","e"
"b","d","e","q"
and query for %"a"%"c"%