Querying a MySQL range using LIKE - php

I have a ref field in my mysql table that holds values that look like '0-0-at-3267-201411041356'. The first part (0-0-at-3267-) varies in length and values and the second part (201411041356) is a date/time that references a creation date/time. Everything that this code is used for is checked to see if it falls within a certain date/time period, such as between 201409010000 and 201508312359.
Normally I can simply explode the data and then measure but for this instance it would make it too clunky. So what I want to do is use the LIKE function in my query like so LIKE '%201411041356' but I want to use it with the > and < symbols, so the full query looking something like SELECT * FROM my_table WHERE ref LIKE > '%201409010000' AND ref LIKE < '%201508312359'
Any ideas would be most welcome! BTW, this has always been the way this data has been stored and there's lots of it so changing it is not an option.

Could you use between on a substring, so something like:
SELECT * FROM my_table WHERE SUBSTRING(ref,-12) BETWEEN '201409010000' AND '201508312359'

SELECT *
FROM my_table
WHERE SUBSTRING(ref,-12) BETWEEN '201409010000' AND '201508312359'`
would work better for substracting last 12 characters.

Because You wrote that first part may have different lenght so You have strip substring starting from the end.
So Your quwery may looks like:
SELECT * FROM table
WHERE
SUBSTRING('ref', -12) > $from
AND
SUBSTRING('ref', -12) < $to

If you just want to measure by date use:
left(right(ref, 12), 8)

Related

mySQL more precision using LIKE function

I'm aware of the LIKE function in SQL, but I need to do something slightly more complex.
In my table people I have a text field called banned which stores a string of all banned names seperated with a #. So let's banned = Roger#Bobjob#Billy
Say I want to check if the name Bob appears
SELECT * FROM people WHERE banned LIKE '%$Bob%'
This would presumably find results because of the Bobjob in the string.
Is there any way I can make it so it only finds full names within the # delimiters?
Forget about using index in either of these.... but since you're using %var% i am assuming you expected that already.
We could concat a # at start and end of banned so that every name is surrounded by #'s then use the # in the name search. (Expects no name to have # in it.... and expects banned to not start nor end with a # (and if they do it really won't matter to the below))
Where concat('#', banned,'#') like '%#Bob#%'
Use find in set by converting the # to a , and if the result is > 1 then it was found.
find_in_set('Bob',replace(banned,','))>1
If you know the list is always composed of 2 or 3 names (as stated in your comment), the most straightforward way is to check for all 3 possible cases:
SELECT *
FROM people
WHERE list LIKE 'bob#%' /* name in first position */
OR list LIKE '%#bob#%' /* name in second position */
OR list LIKE '%#bob' /* name in third, final position */
You should use concat for build a proper like condition with vars and #
SELECT * FROM people WHERE banned LIKE concat('%#%', '$Bob','%#%')
This isn't an ideal data structure, but leaving that aside for the moment:
If you can change the delimiter to a comma, mysql has a built-in function for that: FIND_IN_SET
SELECT * FROM people WHERE FIND_IN_SET('Bob', list);
Otherwise, you can do a regular expression match.
SELECT * FROM people WHERE list REGEXP '(^|#)Bob($|#)';
Neither of these is necessarily very performant, so I wouldn't try it on large data sets.
It's probably not the best db architecture but you might use regular expressions for you purposes like so
SELECT * FROM people WHERE banned RLIKE '(^|#)Bob($|#)'

Search value of a field using substring

I'm working on a project using the pages in php / mysql and html; I have a table that contains the data for calls made from a PBX and save the number called, the source, date, time, etc ... what I want to do is to search within this table all the phone numbers that have the first 4 digits equal to those that pass through the query, only that i have no idea how to pull off only the 4-digit or at least how to make a control character by character of the value contained in the field. I tell you now that the field is a varchar. Thank you in advance :)
To do that in MySQL query, either
SELECT *
FROM <tablename>
WHERE LEFT(<column>, 4) = "<4 digits>"
or
SELECT *
FROM <tablename>
WHERE <column> LIKE "<4 digits>%"
or in the PHP side :
if (strpos($column,'<4 digit>') !== false) {
echo 'true';
}
Use this, to get substring
SELECT aut_name,
RIGHT(aut_name,7)
FROM author
WHERE country='UK';
See more at: http://www.w3resource.com/mysql/string-functions/mysql-right-function.php#sthash.xKNwZeki.dpuf
I suggest this solution:
$variableWhereYoustoreTheFourDigits="1234"; //Use whatever you have in your code to set the value.
$result =$mysqli->query("SELECT number FROM yourtable where number LIKE \"$variableWhereYoustoreTheFourDigits%\");

MySQL select only using first word of variable

I am using php and mySQL. I have a select query that is not working. My code is:
$bookquery = "SELECT * FROM my_books WHERE book_title = '$book' OR book_title_short = '$book' OR book_title_long = '$book' OR book_id = '$book'";
The code searches several title types and returns the desired reference most of the time, except when the name of the book starts with a numeral. Though rare, some of my book titles are in the form "2 Book". In such cases, the query only looks at the "2", assumes it is a "book_id" and returns the second entry in the database, instead of the entry for "2 Book". Something like "3 Book" returns the third entry and so forth. I am confused why the select is acting this way, but more importantly, I do not know how to fix it.
If you have a column in your table with a numeric data type (INT, maybe), then your search strategy is going to work strangely for values of $book that start with numbers. You have discovered this.
The following expression always returns true in SQL. It's not intuitive, but it's true.
99 = '99 Luftballon'
That's because, when you compare an integer to a string, MySQL implicitly does this:
CAST(stringvalue AS INT)
And, a cast of a string beginning with the text of an integer always returns the value of the integer. For example, the value of
CAST('99 Luftballon' AS INT)
is 99. So you'll get book id 99 if you look for that search term.
It's pointless to try to compare an INT column to a text string that doesn't start with an integer, because CAST('blah blah blah' AS INT) always returns zero. To make your search strategy work better, you should consider omitting OR book_id = '$book' from your search query unless you know that the entirety of $book is a number.
As others mention, my PHP allowed both numerical enties and text entries from the browser. My query was then having a hard time with this, interpreting some of my text entries as numbers by truncating the end. Thus, my "2 Book" was being interpreted as the number "2" and then being queried to find the second book in the database. To fix this I just created a simple if statement in PHP so that my queries only looked for text or numbers. Thus, in my case, my solution was:
if(is_numeric($book)){
$bookquery = "SELECT * FROM books WHERE book_id = '$book'";
}else{
$bookquery = "SELECT * FROM books WHERE book_title = '$book' OR book_title_short = '$book' OR book_title_long = '$book'";
}
This is working great and I am on my way coding happily again. Thanks #OllieJones and others for your questions and ideas which helped me see I needed to approach the problem differently.
Not sure if this is the correct answer for you but it seems like you are searching for only exact values in your select. Have you thought of trying a more generic search for your criteria? Such as...
$bookquery = "SELECT * FROM my_books WHERE book_title LIKE '".$book."' OR book_title_short LIKE '".$book."' OR book_title_long LIKE '".$book."' OR book_id LIKE '".$book."'"
If you are doing some kind of searching you might even want to ensure the characters before the search key are found as well like so....
$bookquery = "SELECT * FROM my_books WHERE book_title LIKE '%".$book."' OR book_title_short LIKE '%".$book."' OR book_title_long LIKE '%".$book."' OR book_id LIKE '%".$book."'"
The % is a special char that looks for allows you to search for the chars you want to search for PLUS any characters before this that aren't in the search criteri... for example $book = "any" with a % before hand in the query like so, '%".$book."'"`` would return bothcompanyand also the wordany` by itself.
If you need to you can add a % to the end also like so, `'%".$book."%'"`` and it would do the same for the beginning and end of the search key

Need help in php mysql statement which has "and" and range

I am want to write a php mysql query which includes and and range condition.
In the screen shot you can see the field of the table called search. The fields with same name are the range. I want select query and it should include all the fields and their appropriate range.
The names of the fields are shape1,shape2(it is range from shape1 to shape2) etc and it goes on.
The query should be like this
select * from search where unique_id='$unique_id && (carat1='$carat1' between carat2='carat2') &&...
and there are other field too like cut and shape, all in one query. I am inserting value in to the database directly from android in json format. My problem is that i don't know proper format.
Please help me
It should go like this (an example)
select * from search
where unique_id= 10
and carat1 between 1 and 10
and shape1 between 1 and 10
and cut1 between 1 and 10
EDIT:
If you are trying to run the SQL from PHP script then the format will be different like below (if the column is type integer like unique_id then don't put ' while replacing value. For string type replace with ' like carat)
Select * from search
where unique_id = $unique_id
and carat between '$carat1' and '$carat2'
and color between '$color1' and '$color2'
and shape between '$shape1' and '$shape2'
Example usage of between in mysql:
SELECT * FROM search
WHERE unique_id = 200 AND
carat BETWEEN 1 AND 5;
Note that the AND that follows BETWEEN is used to indicate the range (1,5) and is not a logical AND operator.
Try this, I hope this will help you
SELECT * FROM search
WHERE unique_id = $unique_id AND
carat between $carat1 AND $carat2 AND
color like '%".$color1."%' AND
color like '%".$color2."%' AND
shape like '%".$shape1."%' AND
shape like '%".$shape2."%'

mysql date compare query gives wrong results

I have this table and the query I give returns wrong results, I am not sure where the problem is
the date comparisons
or
structure of the query
The query if not clear in the image is :
select * from transact where item_code='msft234' or item_code='hp550x' and transact_date>=STR_TO_DATE('06-07-2013','%d-%m-%Y') and transact_date<=STR_TO_DATE('12-07-2013','%d-%m-%Y')
Your query employs a wrong syntax:
WHERE item_code='msft234' OR item_code='hp550x'
AND transact_date>=STR_TO_DATE('06-07-2013','%d-%m-%Y')
AND transact_date<=STR_TO_DATE('12-07-2013','%d-%m-%Y')
since AND priority is higher, it means that it will be satisfied if either you get hp550x in that date interval, or you get msft234 regardless of the date.
You have to put the OR'ed item codes in parentheses: (item_code='..' OR item_code='..' OR ..), or use IN: e.g.
SELECT * FROM transact
WHERE item_code IN ('msft234', 'hp550x')
AND transact_date BETWEEN
STR_TO_DATE('06-07-2013','%d-%m-%Y')
AND
STR_TO_DATE('12-07-2013','%d-%m-%Y')
Also, depending on the type you select for the date fields, consider that for a date to be "less or equal than 12-07-2013", it has to be less or equal than 12-07-2013 at 00:00, i.e., almost the latest date that will match is 11-07-2013 at 23:59:59.
So "less or equal than 12-07" will actually never select any row from 12-07-2013 unless it happens to have been inserted exactly at midnight.
If you insert rows by only specifying the date, then it will very probably work - the rows will be input at midnight and matched at midnight. But if (some) rows are entered with the full datetime, e.g. because they're type datetime and updated with NOW(), then they will not match.
put the item conditions between ()
(ítem_code = 'msft234' OR ítem_code = 'hp550x') AND transact_date>=STR_TO_DATE('06-07-2013','%d-%m-%Y') and transact_date<=STR_TO_DATE('12-07-2013','%d-%m-%Y')
(ítem_code = 'msft234' OR ítem_code = 'hp550x') AND transact_date BETWEEN STR_TO_DATE('06-07-2013','%d-%m-%Y') and STR_TO_DATE('12-07-2013','%d-%m-%Y')
For security I will put with 2 ()
(ítem_code = 'msft234' OR ítem_code = 'hp550x') AND (transact_date BETWEEN STR_TO_DATE('06-07-2013','%d-%m-%Y') and STR_TO_DATE('12-07-2013','%d-%m-%Y'))

Categories