Explode data from MySQL field and compare with string - php

I have a problem with data in MySQL.
I have a column "vacation_period" where I keep data with dates separated by commas eg. "02/10/2015,02/11/2015" and I want to explode commas, month and year. I want to make condition where I can compare month and year sent by POST with exploded data from MySQL field.
Example query:
SELECT *
FROM ag_vacations INNER JOIN
ag_employees
ON ag_vacations.user_id = ag_employees.e_id
where ag_vacations.user_id = 1 AND
ag_vacations.vacation_period = 2015 AND
ag_vacations.vacation_period = 02
order by date_added desc

So try this way:
SELECT *
FROM ag_vacations INNER JOIN
ag_employees
ON ag_vacations.user_id = ag_employees.e_id
where ag_vacations.user_id = 1 AND
((ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/2015\,[0-9]{2}\/[0-9]{2}\/[0-9]{4}' AND
ag_vacations.vacation_period REGEXP '02\/[0-9]{2}\/[0-9]{4}\,[0-9]{2}\/[0-9]{2}\/[0-9]{4}')
OR (ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,[0-9]{2}\/[0-9]{2}\/2015' AND
ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,02\/[0-9]{2}\/[0-9]{4}'))
order by date_added desc
so if you need to check just first date in that string 02/10/2015,02/11/2015 - you could delete OR part:
OR (ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,[0-9]{2}\/[0-9]{2}\/2015' AND
ag_vacations.vacation_period REGEXP '[0-9]{2}\/[0-9]{2}\/[0-9]{4}\,02\/[0-9]{2}\/[0-9]{4}')

While a string of dates is not ideal, we sometimes receive data this way. If you need to query this before you have the opportunity to clean up your schema, and if your date formats are consistent, you can query the string with like. In this query, we will concat commas to the beginning and end of the ag_vacations.vacation_period column to make sure we are getting the beginning of the date for month and the end of the date for year:
SELECT *
FROM ag_vacations INNER JOIN
ag_employees
ON ag_vacations.user_id = ag_employees.e_id
where ag_vacations.user_id = 1 AND
concat(ag_vacations.vacation_period,',') like '%/2015,%' AND
concat(',',ag_vacations.vacation_period like '%,02/%'
order by date_added desc

If you just want to find the row, you don't need to explode in php.You can use FIND_IN_SET
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

Related

select where row contains string

I have a column named software_hardware from the table activities which could have multiple product inputs separated by a pipeline like INVENTORY| POS | or GPOS | ACCOUNTING |.
I have this query:
SELECT a.id, a.ticket_number, a.client, a.software_hardware,
a.issues_concern, a.status, a.technical_programmer, a.date_added
FROM activities a
WHERE a.client = '".$_POST['client']."' AND a.software_hardware LIKE '%".$arr[$i]."%'
ORDER BY date_added DESC
$arr = explode("|", $_POST['Soft_hard']);
When I select a client and a product, related info would be displayed on a div. Now my problem is, if I have POS and GPOS, I realized I couldn't use LIKE '%[input product name here]%'as this would return results that contains both POS and GPOS if I selected POS.
How can I fix this query to not display GPOS if I selected POS?
PS. I am aware of the issues with MySQL. It's what the company I'm in uses so I have no choice at the moment.
You can use REGEXP:
a.software_hardware REGEXP '[[:<:]]" . $arr[$i] . "[[:>:]]'
the [[:<:]] and [[:>:]] markers stand for word boundaries. So POS cannot match GPOS as there would be no word boundary at the beginning.
Note you will need to trim($arr[$i]) if it has leading or trailing whitespace.
Try this:
SELECT a.id, a.ticket_number, a.client, a.software_hardware,
a.issues_concern, a.status, a.technical_programmer, a.date_added
FROM activities a
WHERE a.client = '".$_POST['client']."'
AND UPPER(a.software_hardware) LIKE '%".strtoupper($arr[$i])."%'
ORDER BY a.date_added DESC

How to order non-integer records in MySql?

I'm trying to order some MySQL records by DESC order and get the last record added.
These are my records and they're all stored as strings:
1/2017
1/2018
2/2017
2/2018
3/2017
I want to get the value 1/2018. The records are based on the current year
and the last record added was 1/2018 and the next will be 2/2018 and so on.
this is my query:
SELECT NoControl FROM cita ORDER BY NoControl DESC LIMIT 1
but I'm getting 3/2017 as the last record.
If you have ever the same pattern m/yyyy you could use some string manipulation function eg:
SELECT NoControl
FROM cita
ORDER BY right(NoControl,4), DESC left(NoControl,1) DESC LIMIT 1
But you should avoid of store date values as string ..
or convert the string as a proper date
SELECT NoControl
FROM cita
ORDER BY str_to_date(NoControl,'%m/%Y') DESC LIMIT 1
Use SUBSTRING_INDEX to extract the first and second part, CAST as integer and sort:
SELECT str
FROM testdata
ORDER BY
CAST(SUBSTRING_INDEX(str, '/', -1) AS UNSIGNED) DESC,
CAST(SUBSTRING_INDEX(str, '/', 1) AS UNSIGNED) DESC
LIMIT 0, 1
SQL Fiddle

count(*) in each of previous 25 months separately

I have a database name "d" and there is table name "t"...
t has two columns id and month...
I have to calculate no of ids in each month.
actually, I am taking an input in $monthyear, now I want to store count(id) of previous 25 month in a $row
I use this type of commands
$search = "SELECT * FROM `d`.`t` where id>2";
$result = mysqli_query($con, $search);
$row = mysqli_fetch_array($result);
please tell me a query which can do this trick and can you store it like
$row[0]="no of ids in the inputed month"
$row[1]="no of ids in the [inputed month - 1 month]"
$row[2]="no of ids in the [inputed month - 2 month]"
and so on....
note: both month and "years" do matter.
If not all month has IDs , then you need to use a derived table. Something like this:
SELECT t.monthyear, COALESCE(COUNT(s.monthyear),0) as numOfID
FROM(SELECT '012016' as monthyear
UNION ALL
SELECT '022016'
....) t
LEFT JOIN YourTable s
ON(t.monthyear = s.monthyear)
GROUP BY t.monthyear

match date in mysql and give result

I have written query in mysql as
select * from tblstafftasks as tst join tblstafftaskassignees as tsta on tsta.taskid = tst.id join tblstaff as ts on ts.staffid = tsta.staffid where tst.startdate like '2016-07-21' or '2016-07-21' between tst.startdate and tst.duedate and tst.rel_id = 1
My database looks like :
When I have written above query it runs perfect but does not give any result.
Here I have used 2016-07-21 that match with the startdate so it not give any result. So what query should I have to write to get result if it match with the startdate.?
Here i pass date in simple Y-m-d format and in database it store as datetime so help me to solve this query.
It seems like you want to compare a date with DATETIME data types. If you want to compare just with the date of the DATETIME field, do something like this:
SELECT * FROM tblstafftasks AS tst
JOIN tblstafftaskassignees AS tsta ON tsta.taskid = tst.id
JOIN tblstaff AS ts ON ts.staffid = tsta.staffid
WHERE DATE(tst.startdate) <= '2016-07-21'
AND DATE(tst.duedate) >= '2016-07-21'

How to use DATEDIFF? How many days are inside of two dates

How to use DATEDIFF? How can I make this to work? or should I use DATEDIFF completly differently?
SELECT DATEDIFF('Started ','will_end') AS 'Duration' FROM my_table WHERE id = '110';
I try to get answer, how many days are inside of two dates.
I would like to get an aswer like:
Duration = 7 days;
I have this kind of database:
Started | will_end
2009-12-17 | 2009-12-24
2009-12-12 | 2009-12-26
Put will_end first, started second:
SELECT DATEDIFF('2009-12-24', '2009-12-17')
---
7
Also, remove the single quotes from your field names:
SELECT DATEDIFF(will_end, started) AS Duration
FROM my_table
WHERE id = 110
, or replace them with the backticks:
SELECT DATEDIFF(`will_end`, `started`) AS `Duration`
FROM `my_table`
WHERE `id` = 110
Are you getting a NULL result? You have the column names in single quotes in your query, which means you are passing the strings 'Started ' and 'will_end' to DATEDIFF rather than the column values. Try removing the single quotes, and you will start to see some results:
SELECT DATEDIFF(Started, will_end) AS Duration FROM my_table WHERE id = '110';
Note that this will give you a negative result. To get a positive result, reverse the order of the columns:
SELECT DATEDIFF(will_end, Started) AS Duration FROM my_table WHERE id = '110';
replace the order
DATEDIFF('will_end','Started')
I think there are 3 parameter to be passed in
DATEDIFF ( datepart , startdate , enddate )
so your code would be
DATEDIFF ( dd , 'Started ','will_end' )
http://msdn.microsoft.com/en-us/library/ms189794.aspx

Categories