Searching Large Mysql Database For Exact Date Within Row ID - php

I have a large mysql database which is about 10gb large. One of the tables in the database is called
clients
In that table there is a colum named
case
The date this client is created is mixed into the number within this column.
Here is an example of an entry in case
011706-0001
The 06 part means this client was created in 2006. I need to pull all the clients that were created in 2015 and 2016. So I need to query for anything that case has a 15 or 16 before the dash.
For example, 000015-0000 or 000016-0000
Is there a way to do this with only mysql? My thought process was I would have to query the whole column then use php to preg_match()
I am worried that based on the size of the database this would cause problems.

To locate rows that have a case column value that contains '06-' (the characters 0 and 6 followed by a dash ...
One option is to use a LIKE comparison operator:
SELECT ...
FROM clients t
WHERE t.case LIKE '%06-%'
ORDER BY ...
The percent sign characters are wildcards in the LIKE comparison, which match any number of characters (zero, one or more.)
MySQL will need to evaluate that condition for every row in the table. MySQL can't make use of an index range scan operation with that.
SELECT ...
FROM clients t
WHERE t.case LIKE '%15-%'
OR t.case LIKE '%16-%'
ORDER BY ...
That will evaluate to true for any values that include the sequence of three characters '15-' or '16-'.
If there's a more standard format for the values in the case column, where the value always starts with exactly six characters representing date 'mmddyy-nnnnn' and you only want to match the 5th thru 7th characters, you could use the underscore wildcard character which matches any one character (in the LIKE comparison) for example... using four underscores
t.case LIKE '____16-%'
Or you could use a SUBSTR function to extract the three characters from the case value, and perform an equality comparison...
SUBSTR(t.case,5,3) = '15-'
SUBSTR(t.case,5,3) IN ('15-','16-')
It's also possible to make use of a REGEXP comparison in place of the LIKE comparison.
In terms of performance, all of the above approaches are going to need to crank through every row in the table, to evaluate the comparison condition.
If that date value was stored as a separate column, as a DATE datatype, and there was an index with that as the leading column, then MySQL could make effective use of a range scan operation, for a query like this...
WHERE t.casedate >= '2015-01-01'
AND t.casedate < '2017-01-01'

Related

Regular Expression where a number is less than 10

I have a table with a column where I have data stored this way:
1:29,3:20,5:0,4:0,2:76
I want to make a query request in PHP (or MySQL in general) and get rows where there is a number less than 10 after the " : ".
Therefore, in this case I have ( 29 , 20 , 0 , 0 , 76 ), and because I have a number less than 10, I will want to take this row.
In general, storing comma separated values is not considered a good design in SQL, as it leads to violation of data normalization rules and in addition faces many other issues. But since you have already done it and want a regex solution to your problem, you can use this regex,
:[0-9](,|$)
Here is a demo
In mysql you can write a query like this,
select * from tablename where columnname regexp ':[0-9](,|$)';
This will give you rows where at least one comma separated value is less than ten following a colon.

How to display the column value descending when the column having spacial characters in mysql

How to display the column desc order when the column having spacial chars in mysql
I am using the follow query but not display correctly
SELECT quotation_pno FROM crm_quotation order by quotation_pno desc
My output coming like this
quotation_pno
PT/17/999
PT/17/1533
PT/17/1532
PT/16/1531
I want my output like this
quotation_pno
PT/17/1533
PT/17/1532
PT/17/999
PT/16/1531
Please help me
I'd argue, that the output is correct, but your assumptions are not. It looks to me, as if quotation_pno is some kind of textual column, right?
The sorting assumes, that you want to sort text and this works this way:
Set i to 0
Compare the i-th character of two strigns
If they are the same and the end is not reached, increase i by 1 and proceed with step 2
Otherwise order the two strings according to the value at the i-th position
(There are some things elided and the pseudocode is boiled down to the very basic, needed to understand the principle).
Applied to your example this means, when the comparison compares PT/17/999 and PT/17/1533 it looks at the characters 0 to 5 and "sees" that they are equal. When it compares the characters at position 6, they are '9' and '1'. Since the character '9' is considered to be greater than '1', PT/17/999 is placed before PT/17/1533.
How to solve the issue?
There are some ways coming into my mind, that will allow you to achieve the desired sort order.
First, you could prepend the numbers with zeros. This will allow you to re-use most of your existing structure, but will result either in very many zeros, or a system that is somehow limited, since you will be restricted to the number of digits you decided to use (or the sort will fail again).
The second possibility is, to store the parts in (additional) numerical columns in the table, e.g. one for year and one for the order number in this year. This is the more flexible approach, but involves more changes.

What is a proper length of random code used as mysql select / join attribute

I assign each mysql row with a random code. Only alphanumeric lowercase characters and numbers are used.
These random codes are used for SELECT and JOIN queries.
Most often I use length of 24 characters which gives roughly 2.2x10^37 available combinations.
What is the perfect random code length that does not require checking for duplicates while being certain no collisions will occur but also keeping query result time to minimum?
Note: On big tables I use INDEX for the random code column of length 24.
The nature of a RANDOM code is that you can never be certain it will not be duplicated. You can guarantee there wont be a duplicate if you use a sequential number & if you are doing that you might as well use the AUTO INCREMENT option with a sufficiently large integer.
http://dilbert.com/strip/2001-10-25
It is not possible to generate a random code which might not be duplicated, therefore you must always check for duplicates and there is no "perfect random code length".

Zend Searching for serial numbers (strings not integers)

I have some search functionality currently working in which I can search for serial numbers aslong as they are integers.
For example this works fine.
Search for serials >= 352123 and <= 360000
What is the best approach for doing a similar search when serials can consist of a string?
For example:
= '352123/230w' and <= '352123/250w'
I have my table set up as MyISAM to make use of fulltext search in the hope that this will help in this case?
You can do string comparisons like this, but you have to be aware that it's using the lexical value. eg:
SELECT '352123/230w' <= '352123/250w';
-- returns 1
SELECT '352123/230w' <= '4';
-- also returns 1
Assuming that all of your serial numbers are formatted like this you could use string operations to split out the numbers, convert to integers, and sort based on those, however you're both introducing a lot of conversion overhead as well as throwing away your indexes, so it would be very slow/inefficient.
You could always add another field or two to store a numerical equivalent, ie. '352123/230w' stripped of non-numeric chars to 352123230 and use that for your sort, but it really depends on the formatting of all tracked serial numbers being consistent.

PHP, find exact value from mysql arranged data

I have a data in My-Sql column like this
T_interest
1,14,49,145,203,302
It represents each value for personal interest keywords.
I tried to extract the value and distinguish whether it has the value or not for the checkbox.
if(strstr($u_interest['u_interest'], ','.$row['i_idx'])):
$selected = 'checked';
here is the php command that I use right now.
but it doesn't extract exact value from the database.
Let's say I want to check if the data has 14 number or from this user table
T_interest
1,14,49,145,203,302
and if I use above command it tells me that a user has two values.
14,145
It looks like PHP strstr command tells me two values because these two have 14 number.
So, can you help me why this is happening?
If you want more php lines I can post them.
Explode it into an array, this way you get the individual values.
explode(",",$t_interest["u_interest"]);
Then you can test for equality much easier.
You really need to read up on database normalization. A properly normalized design would make this problem moot.
As for your question, since you're forced to use string operations, you'll have to check for multiple different cases:
1) the number you want is at the START of the string
2) the number you want is at the END of the string
3) the number you want is the ONLY number in the string
4) the number you want is in the MIDDLE of thee string:
SELECT ...
WHERE
(T_Interest = 14) OR // only number
(T_Interest LIKE '14,%') OR /// at the begnning
(T_Interest LIKE '%,14') OR // at the end
(T_Insertest LIKE '%,14,%') // in the middle

Categories