Good day.
I have row in database:
When i make first select:
SELECT * FROM `table` WHERE `name` Like '%Коляска \"Balmoral\" Silver Cross%'
i get null(num rows = 0)...
But if i make second select:
SELECT * FROM `products` WHERE `name` Like '%Balmoral%'
i get num rows = 1.
Why not work first select and how make right select?
Slashes in database and in select becose i use mysql_real_escape_string when i insert new row and use select
The backslash is an escape character. If you want to look for it you need to escape it. (In other words, you need to escape the escape character.):
SELECT * FROM `table` WHERE `name` Like '%Коляска \\"Balmoral\\" Silver Cross%
Use this:
SELECT * FROM `titles` WHERE `title` LIKE '%Коляска \\\\"Balmoral\\\\%" Silver Cross'
Explanation: the \ must be escaped with \ for SQL, but for PHP we need extra \ for escaping also.
Or Simply:
SELECT * FROM `titles` WHERE `title` = 'Коляска \\"Balmoral\\" Silver Cross'
However, I don't know why!!!
Like not good idea in this operation. Need use fulltext.
Example
SELECT *, MATCH (name) AGAINST
('Коляска \"Balmoral\" Silver Cross') AS score
FROM products WHERE MATCH (name) AGAINST
('Коляска \"Balmoral\" Silver Cross');
Related
I try to make SQL to search some string in database.
In this spesification, The SQL must be dont display one string in database.
my sql like this :
$query = "SELECT * FROM `chatuser` WHERE CONCAT( `fullname`,`image`) LIKE '%".$search_string."%' NOT (`$string is not be displayed`) " ;
is that possible ?
Thanks for help
The correct syntax of LIKE and NOT LIKE as two conditions would be:
SELECT * FROM chatuser
WHERE CONCAT(CustomerName,ContactName) LIKE '%t%'
AND CONCAT(CustomerName,ContactName) NOT LIKE '%m%';
You miss AND Between conditions. Also you have to repeat CONCAT(CustomerName,ContactName).
In the example above we are looking for all CustomerName+ContactName with a t in any place but if it doesn't have an m in any place.
From the docs found at https://www.w3resource.com/mysql/comparision-functions-and-operators/not-like.php
Example: MySQL NOT LIKE operator with (%) percent
The following MySQL statement excludes those rows from the table author, having the 1st character of aut_name ‘W’.
Code:
SELECT aut_name, country
FROM author
WHERE aut_name NOT LIKE 'W%';
And so it seems would work in your situation.
I'm getting the error: Incorrect syntax near the keyword 'AS'.
I'm trying to select a column from my MSSQL database while NOT SELECTING the first character of that field.
Below is the code (Within PHP):
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) FROM Table WHERE [Column] ='".$search."' AS Column2";
First of all. Please be careful. Your code looks vulnerable to an SQL injection. Better use paramterized queries.
The second "AS" at the end is not needed.
Try it that way:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS Revlv2 FROM table_name WHERE [Objkt] ='".$search."'
Or better yet:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS column_name FROM table_name WHERE [Objkt] = ?
Read more about parameterized queries here
Also take care when the Revlv column only contains an empty string. The query will fail in that case.
try this:
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
As all answers simply fix the syntax instead of fixing the logic:
There's no need to use RIGHT + LEN to extract everything but the first character, simply use
substring(Revlv from 2) AS Revlv2 -- Standard SQL to extract everything after the first character
As SQL Server has a slighty different syntax:
substring(Revlv, 2, 8000) AS Revlv2 -- T-SQL to extract everything after the first character
You need to put AS after the SELECT part of your query never put it at the end of your query. Try to do it like this
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
SOURCE
try this.
$sql="SELECT RIGHT(Revlv, LEN(Revlv)-1) AS Revlv2
FROM tablename where['objkt']='$search'";
The script fetches input from user and searches database field, named tags.
tags contains data for eg: hello, hey, how , happy, hell .. on single cell or you may say as a string.
i tried the query below, but it only works for hello and not for how , happy, hell..
$sql ="SELECT * FROM tws where tags='".$name."' or tags LIKE '".$name.",%' or tags LIKE
',%".$name.",%' or tags LIKE '%,".$name."'";
Note: I thoroughly searched google and stack overflow before posting this question but couldn't get it to work.
Use the following query:
$sql = "SELECT * FROM `tws` WHERE `tags` REGEXP (".$name.")";
You have one percent-sign placed wrong (the third LIKE) and MySQL is quite strict: you have to include the spaces between your tags in the SQL:
$sql ="SELECT * FROM tws where tags='".$name."' or tags LIKE '".$name.",%'
or tags LIKE '%, ".$name.",%' or tags LIKE '%, ".$name."'";
should work (remove the space after how, so your data has a fixed format).
PS: Make sure you use mysql(i)_real_escape_string to escape the value $name
Consider using an extra table where you store your tags. e.g. tws_tags, with fields tws_id and tag and a query with a JOIN on that.
You should omit the commas in your query like this:
$sql ="SELECT * FROM `tws` WHERE `tags` RLIKE '%[[:<:]]$name[[:>:]]%'";
[[:<:]] and [[:>:]] are special markers in MySQL for word boundaries. Alse, in double quotes you can use variables without having to concatenate ("blablabla $smth txt" instead of "blablabla " . $smth . " txt")
Use this:
$sql ="SELECT * FROM tws where tags='".$name."' or
tags LIKE '".$name."%' or tags LIKE
'%".$name."%' or tags LIKE '%".$name."'";
Here your query looks like below if name = ABC
$sql ="SELECT * FROM tws where tags='ABC' or tags LIKE 'ABC,%' or tags LIKE
',ABC,%' or tags LIKE '%,ABC'";
You can see that it is looking for ,ABC not only for ABC
I want to write a query which name having first character having a or b or c or c-g.
I have list of name with alphabetical order A-Z. i want to filter the by alphabetical order in 3 steps A-G, G-M, N-Z.
By clicking A-G the record shows the name which first character starting from A-G
mysql_query("select * from users where name like "A%" or name like "B%" or name like "C%" or name like "D%" or name like "E%" or name like "F%" or name like "G%"");
But i don't want to write the like several time
so is there any easy way instead of writing like several time.
You can try
mysql_query("select * from users where LOWER(SUBSTR(name, 1, 1)) IN ('a','b','c','d','e','f','g')");
REGEXP could work well here:
SELECT ........ WHERE `name` REGEXP '^[a-g]'
Alternatively, to make better use of indexes:
... WHERE `name` < 'G'
There are ways to achieve this:
First: you can do it with .. OR LIKE .. syntax, like you've described.
Second: use SUBSTRING() to pass into IN operator:
SELECT * FROM t WHERE SUBSTRING(name, 1,1) IN ('A', 'B', 'C')
or use ORD()
SELECT * FROM t WHERE ORD(SUBSTRING(name, 1,1)) BETWEEN ORD('A') AND ORD('C')
Third: use REGEXP:
SELECT * FROM t WHERE name REGEXP '^[ABC]'
i mean i have songs table ok??
i want to make some things like that
a | b | c | d
click on a select from the database the songs that start by a Character
how i can do that by the mysql???
I believe you can use a LIKE statement to achieve this.
SELECT * FROM `table` WHERE `title` LIKE 'a%'
That will select anything where the title starts with an 'a', the % allows for anything after that. I'm not sure if this is case-sensitive or not (I don't believe it is), can someone confirm either way?
Retrieved from: http://www.webmasterworld.com/forum88/2320.htm
select * FROM `table` WHERE LEFT(`value`, 1) NOT BETWEEN 'A' AND 'Z'
Here it is in Python because I can't be bothered to look up the crappy PHP syntax:
qry = "select song_name from songs where left(song_name, 1) = '%s'" % letter
This assumes that you haven't foolishly used a case sensitive collation as well. In case you were being silly and did that well:
qry = "select song_name from songs where left(lower(song_name), 1) = '%s'" %
letter.lower()