i have two tables of businesses , i need to select(search) between tables by business name.
for example: if in one table i have business with name "Alcantara furniture network" with id 1, in another table the name "Alcantara furniture" with id 2 . the id 2 should be returned when searched from the first table with id 1.
i have tried to write query with LIKE but it is not helpful because it won't return the other business(id 2) . how can i search a substring in a string in mysql ?
You should change your like query to select what is common about the 2 values, i.e. 'Alacantara furniture':
select bussid,name
from tempBusiness
WHERE name LIKE '%Alcantara furniture%'
the string 'network' only appears in one of the names, so WHERE name LIKE '%Alcantara furniture network%' will only match one.
Related
select name from students where name like '%A&M%';
above query is not giving results whose name contains A&M instead it giving whose name as A(any character)M in name
but when i run this select name from students where name like '%A&MOhan%'; it is giving students whose name contains A&Mohan
I am using hibernate createNativeQuery
You need to escape the input for special characters with a backslash, so for example:
select name from students where name like '%A\&M%';
Should give you the correct result
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
A table field has values has 1,2,3 (comma seperated value) and a variable has 2,3. Now i need to check weather variable value is in table field using query along with another name field
user table
id name cat_id
-----------------
1 test 1,2,3
2 test1 3,4
3 test2 4
variable $value = 2, 3
Query : select * from user where name='test' and cat_id IN ('".$value."')
but for above query i get zero data
How to check if given id is exist in cat_id field and name exist in table
You can use a regex to check whether the value is contained in cat_id:
SELECT * FROM user WHERE name='test' AND cat_id REGEXP CONCAT('[[:<:]]', value, '[[:>:]]')
this will attempt to match value at any word boundary in cat_id, so for cat_id='1,2,3', values of (for example) '1,2', '2', '2,3' will match.
To put it in a string form (e.g. for PHP):
$sql = "SELECT * FROM user WHERE name='test' AND cat_id REGEXP CONCAT('[[:<:]]','" . $value. "', '[[:>:]]')";
Your cat_id field should contain only one id by row.
It's normal that your SQL request doesn't work currently because you're looking for cat_id 2 or 3 which SQL is not finding.
For example in your first row 1,2,3, for MySQL it's a string "1,2,3" and not an array of three ids.
If a name can be used by several cats maybe they should be the ones having a name_id.
And if a cat can have several names, and a name can have several cats, you should create a new table cats_names containing one name_id and one cat_id by row.
I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?
I have name,surname,city,address,mobile fields in mysql table.
I want a select statement that select all the records if any of the above fields data matches;
For eg: If i put name: yusuf. then select statement should look data :yusuf in all column mentioned above and if any of the record matches then it should show the result.
Lets say you search by name.
Then use SELECT * FROM table WHERE name LIKE userinput.
Hope this help
select name,surname,city,address,mobile from Table
where name like '%Yusuf%'
Or Surname like '%Yusuf%'
or City like '%Yusuf%'
Or Address like '%Yusuf%'
Or Mobile like '%Yusuf%'