MySQL DELETE query works except not in PHP - php

I have a MySQL query to delete 'near' duplicate rows from a table, and while using test data outside of my project, the query appears to work as intended. When I use the same query with PHP in the project, I get an SQL error. I've been trying all sorts of different combinations of quotes and backticks and I can't seem to get this working.
Any idea what is going on here?
Problem being solved:
This table sometimes will have rows that are nearly identical, with the only exception being the as_of_date column and the total. Only the the most recent date is important, and any older data is no longer needed in this table once newer data comes in.
Table structure with example data:
+----+---------+------+-------------+-------+
| id | account | year | as_of_date | total |
+----+---------+------+-------------+-------+
| 1 | 123 | 2017 | 2017-02-02 | 250 |
| 2 | 123 | 2017 | 2017-11-24 | 790 |
| 3 | 123 | 2018 | 2018-01-30 | 55 |
| 4 | 456 | 2016 | 2016-04-04 | 500 |
| 5 | 456 | 2016 | 2016-10-10 | 300 |
| 6 | 456 | 2017 | 2017-03-12 | 44 |
| 7 | 789 | 2015 | 2015-12-23 | 2000 |
+----+---------+------+-------------+-------+
Expected Outcome:
The desired result is to delete all 'near-duplicate' rows in the table except for the most recent one (as_of_date). So there should only be at most 1 row for any given account and year. The table should look like this after the query is executed:
+----+---------+------+-------------+-------+
| id | account | year | as_of_date | total |
+----+---------+------+-------------+-------+
| 2 | 123 | 2017 | 2017-11-24 | 790 |
| 3 | 123 | 2018 | 2018-01-30 | 55 |
| 5 | 456 | 2016 | 2016-10-10 | 300 |
| 6 | 456 | 2017 | 2017-03-12 | 44 |
| 7 | 789 | 2015 | 2015-12-23 | 2000 |
+----+---------+------+-------------+-------+
The query:
$query = "DELETE FROM `my_table` AS t
WHERE t.as_of_date NOT IN (
SELECT MAX(as_of_date)
FROM (SELECT * FROM `my_table`) AS t2
WHERE t2.account = t.account AND t2.year = t.year
GROUP BY account, `year`
)";
Here is the SQL error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'AS t
WHERE t.as_of_date NOT IN (
S' at line 1

Don't use table aliases in DELETE FROM. That is, try DELETE FROM my_table WHERE..., omitting AS t.
By the way, the only times you really need backticks are when you have table names that are the same as reserved words or have spaces in them.
SELECT * FROM `SELECT`
or
SELECT * FROM `My Favorite Table`
Wise programmers avoid those situations.

Related

Fetch results in group based on the occurrence

Ok, I have a single MySQL table with the name 'car' and 3 columns.
+----+--------+------------+
| ID | car_id | engine |
+----+--------+------------+
| 1 | 123 | on |
| 2 | 123 | on |
| 3 | 123 | off |
| 4 | 123 | on |
| 5 | 123 | on |
| 6 | 123 | on |
| 7 | 123 | off |
| 8 | 123 | on |
| 9 | 123 | off |
+----+--------+------------+
Now I want to show the trips this car did. The trips would be determined based on car engine start and stop. For example from the above example we can see that user has made 3 trips as total(From on to off). Now What I want is that if there is a query which gives me only 3 results from on to off meaning if somehow the query groups the records by considering a starting point on and ending point off. Is it possible in mysql? or the other way around is doing it manually by fetching all the records and working in arrays?
At the moment I am fetching all the records and doing it manually by looping all the data and doing accordingly But this process is slow.
Can you try it ?
SELECT * from cars WHERE `engine` = 'off' AND id IN(SELECT id+1 FROM `cars` WHERE `engine` = 'on')

Embed external variable into sql select result

I have this particular use case where I have to add a string as a particular field one time for a row in an sql result set.
E.g. :-
I have the following result set:-
| id | Name | Year |
| 1 | ABCD | 2017 |
| 2 | EFGH | 2017 |
I have two string { a , b } which I need to embed into this result set as such without affecting the base table:-
| id | Name | Year | String |
| 1 | ABCD | 2017 | a |
| 2 | EFGH | 2017 | b |
How can I do this with php? or better, with sql?

MySQL Search Double with Currency Format

I have a PHP Based App that stores invoices entered by the user. Currently I have the invoice amount stored in a MySQL database tables as a double like so:
+------+--------------+--------------+----------------+----------------+-------------+-----------+---------------+-------------+-------------+-----------------+
| id | date_entered | invoice_date | invoice_number | invoice_amount | client_type | unique_id | supplier_type | supplier_id | category_id | childcare_hours |
+------+--------------+--------------+----------------+----------------+-------------+-----------+---------------+-------------+-------------+-----------------+
| 1 | 1411098397 | 1411048800 | 123 | 0.01 | 0 | 137 | 0 | 139 | 5 | NULL |
| 2 | 1412123404 | 1416920400 | 5093 | 130 | 0 | 168 | 0 | 19 | 18 | NULL |
| 3 | 1412125933 | 1412085600 | 000 | 79 | 0 | 151 | 0 | 177 | 8 | NULL |
| 4 | 1412645652 | 1412600400 | 000 | 60.8 | 0 | 104 | 0 | 179 | 9 | NULL |
| 5 | 1412647563 | 1409320800 | 804560 | 225.5 | 0 | 18 | 0 | 174 | 10 | NULL |
I am also using DataTables toorganise the data. I am using Server Side Processing to perform the data lookup to return as JSON.
The issue I am having is that the User is attempting to search by price eith by typing $123 or 123.50 This is not working as the SQL is being genrated like so: SELECT * FROM invoices WHERE invoice_amount LIKE "%$123%";
This is obviosuly failing due to the data being stored in the database as a double.
My Question is, is there a way to make the SQL (or Maybe PHP) search for the correct value no matter what the client types in?
I don't think there is any generic solution for the problem that you are facing but yeah you can remove the special characters like $ etc. from the beginning or end of the invoice amount to be placed in the query. Moreover I'll recommend that you should use functions as round in PHP as well as MySQL in the best possible fashion rather than using the LIKE statement. Using LIKE statement is absolutely incorrect in this situation.
You can try query without "$"
SELECT * FROM invoices WHERE invoice_amount LIKE "%123%";

Select rows from specific date with left join

I'm currently working on a simple employee scheduling tool and have a problem with a SQL query. To explain my problem let use this two very abstract tables.
The first table simply consists of the employees
employees
======================
empId | name | ...
----------------------
10 | Scott |
11 | Schrute |
12 | Halpert |
13 | Howard |
In the second table you find the assigned tasks to each employee by day.
tasks
==============================================
tasId | name | task | date | ...
----------------------------------------------
10 | Scott | Support | 2014-02-17 |
11 | Scott | Bugfix | 2014-02-18 |
12 | Halpert | Bugfix | 2014-02-17 |
13 | Halpert | Develop | 2014-02-18 |
14 | Howard | Support | 2014-02-17 |
Now I want to know what the employees are working on on Feb 17th or if they have no tasks planned for that day. I use the following SQL query to do that.
SELECT e.name, t.task
FROM employees e LEFT JOIN tasks t ON e.name = t.name
WHERE date IS NULL OR date = DATE('2014-02-17')
The result delivers exactly what I need:
name | task
--------------------
Scott | Support
Schrute | NULL
Halpert | Bugfix
Howard | Support
And now to my problem. If I want to see the tasks of Feb 18th I get this result set:
name | task
--------------------
Scott | Bugfix
Schrute | NULL
Halpert | Develop
The reason to this is obvious, the date of Howard's tasks are neither NULL nor do they equal 2014-02-18.. What would be the best way to get the desired result?
I use MySQL and PHP.
(Sorry for the stupid title, I couldn't think of anything better..)
Move your filter to the join predicate:
SELECT e.name, t.task
FROM employees e
LEFT JOIN tasks t ON e.name = t.name AND t.date = DATE('2014-02-18');
Your query as it is will only return people who have no tasks at all, or have tasks on the date specified. It will omit people who have tasks, but not on the date specified. Consider the results of joining your sample data with no where clause:
empId | name | task | date | ...
----------------------------------------|
10 | Scott | Support | 2014-02-17 |
10 | Scott | Bugfix | 2014-02-18 |
11 | Schrute | NULL | NULL |
12 | Halpert | Bugfix | 2014-02-17 |
12 | Halpert | Develop | 2014-02-18 |
13 | Howard | Support | 2014-02-17 |
As you can see there is no record for Howard where Date = 2014-02-18, or where Date is null, this is why no record is returned for Howard. When you add the filter to the join predicate your results become:
empId | name | task | date | ...
----------------------------------------|
10 | Scott | Bugfix | 2014-02-18 |
11 | Schrute | NULL | NULL |
12 | Halpert | Develop | 2014-02-18 |
13 | Howard | NULL | NULL |
Which I think is the desired results.
You can use
UNIX_TIMESTAMP(`date`) = 0
Looks like you have some data which are stored as '0000-00-00' and its not null so adding the extra OR condition as above will return those data as well.

MySQL Delete Rows With Duplicate and Similar Content

I am new for PHP and MYSQL. In my study I met a problem
I have a table like this:
+----+----------+---------+
| time | name | number |
+----+----------+---------+
| 12.1 | google | 10 |
| 12.2 | yahoo | 15 |
| 12.3 | msn | 20 |
| 12.1 | google | 10 |
| 12.1 | google | 29 |
| 12.2 | yahoo | 10 |
+----+----------+---------+
but I want the talbe like this:
+----+----------+---------+
| time | name | number |
+----+----------+---------+
| 12.2 | yahoo | 15 |
| 12.3 | msn | 20 |
| 12.1 | google | 29 |
+----+----------+---------+
when the time and the name are the same, I want the row with the max number,
what should I do? I am very worrying about this problem and thank you for anwsing me
TRY ( not tested )
SELECT `time`, `name`, `number`
FROM tbl
GROUP BY `name`,`time`
HAVING MAX(`number`)
tip : TIME is a mysql reserve keyword so wrap it with ` and create Index on name and time column together
Try it.
Select time, name, number from [table name] group by time, name having max(number)

Categories