auto-update my table using phpmyadmin trigger or bash script - auto-update

This is what I want to do...
1. read table
2. if UPDATE_REQ is not equal to 0000-00-00 00:00:00 or NULL
then
do move data from infonew to infomain
do delete data from infonew
do reset value of update_req to null
3. else end script
TABLE looks like this...
shopname | infomain | infonew | date
ABC | Location | New location | 2014-01-01 10:00:00
XYZ | Location | Null | 0000-00-00 00:00:00
Please help me which is the best solution (Create a trigger in phpmyadmin, create bash script or create php script)
Thanks! I am looking forward to your response.

Related

MySQL event schedule from another table's dates

Is it possible to run MySQL event based on dates stored in table?
For example if I have table like
| id | date |
|----|------------|
| 1 | 2016-05-20 |
| 2 | 2016-05-25 |
| 3 | 2016-06-15 |
...
can I run event automatically at 2016-05-20, 2016-05-25 and 2016-06-15?
Your daily event should check that table and run its code if today's date is in it !
Yes, It's possible to run event automatically at 2016-05-20, 2016-05-25 and 2016-06-15 dates by using cron-job.
You can run daily event, check with your table date and run its code.
Suppose you want to automatically run your script file at some dates, you need to set cron-job using php. Ref How to create cron job using PHP

Update and add data in sql from multiple excel files

Let's say I have the following sheet on an excel file:
KEY | ID | Account | Value
01002 | 01 | 002 | 10
As you can see, "KEY" is result of combining "ID" and "Account"
Now, I know how to import a table into an SQL form. I also know how to replace it. But I'm having trouble to add and update SQL table from an excel file.
For example,
If after adding this to an SQL table:
KEY | ID | Account | Value
01003 | 01 | 003 | 10
02008 | 02 | 008 | 11
I then update this :
KEY | ID | Account | Value
01003 | 01 | 003 | 14
02009 | 02 | 009 | 10
key 01003 will be updated with the new value, while key 02009 will be added as a new entry. key 02008 will stay as it is.
Is there any way to do this?
If so, is there any way to run this on multiple workbooks (let's say, multiple workbooks in a folder)
I'm fine with VBA (excel add-in)
One way I could think about the latter one is using the same macro that was used to merge several workbooks into one, but replacing the query to update the excel file with whatever query that can do the above thing (update or add)
I would recommend checking out Sequel pro. It has a great import function that walks you through everything. As well as set the delimiters nice and easy!
I believe it will solve your key issue.
If you need help further comment and ill help you as much as I can
http://www.sequelpro.com/

MySQL: Finding existences between values in database and array

I'd like to know how can I make a unique query to find which values exist and which values do not. I explain.
What I have
I've got a database table with a structure as follows:
+----+--------+-----------+-----------+
| id | action | button_id | type |
+----+--------+-----------+-----------+
| 1 | 1 | 1 | button |
| 2 | 2 | 4 | button |
| 3 | 1 | 2 | attribute |
+----+--------+-----------+-----------+
As you can see, an action can have multiple button_id values. For your knowledge, a button_id can be assigned to multiple action, too, but a button_id can only have a type for an action.
So, button_id 1 can be also present in action 4 with the type "attribute" set to it, but it cannot be duplicated to the same action with another type.
The problem
The problem comes when I want to update the buttons in an action. I receive an action object with an array of the buttons it have (in PHP) with the structure below (I write it in JSON structure):
"buttons":
[
{
"id":"1",
"type":"button"
},
{
"id":"3",
"type":"attribute"
}
]
As you can see, the button with ID 1 remains the same, but I've got a new button to deal with (the button with ID 3) and the button with ID 2 is not present anymore.
What I'd want
I'd want to be able to make a unique MySQL query that returns me which values from those I receive exists and which do not, and which may be present in the database but not in that array.
To sum up: I want to know the differences between the buttons in the array received and those present in the database.
So, as an example with the received data described before and the database as we have it right now, I expect to receive something like this:
+--------+-----------+--------+------------+
| action | button_id | exists | is_present |
+--------+-----------+--------+------------+
| 1 | 1 | 1 | 1 |
| 1 | 2 | 1 | 0 |
| 1 | 3 | 0 | 1 |
+--------+-----------+--------+------------+
With this information, I'd be able to know that button with ID 2 does not exist anymore (because it's not present in the new array) and button with ID 3 is a new button because it does not exists previously but it's present in the new array.
What I've tried
There are some tests I've tried, but none of them gives me what I need, and not only tested with MySQL pure queries.
For example, I've tried to check the existence for each button I receive but that would leave me without being able to find if a button is deleted (so it's not present in the received array).
Checking that but taking as reference the buttons in the database has the same effect, as I will be able to check which have been updated or deleted, but it would skip those that are new and not present in the database.
I've tried to write some queries making COUNT queries and GROUP BY button_id, and so, but no luck neither.
(I won't write the queries because none of them have given me the expected results, so they won't be of any help for you).
Any combination of those explained before I think will be much slower than doing it purely by database queries, and that's why I'm asking for it.
The question
Is there a query that would return to me something like the response explained before in "What I'd want" section, so it would make only a call to the MySQL server?
Thank you all for your time, your responses and your patience for any lack of information you may find by my part.
Of course, any doubts, questions you have or information you may need, comment it and I'll try to explain it better or to add it.
Kind regards.
To do that in a single query would be very cubersome. Here is a solution that is not exactly what you are looking for but should do the job.
Let's say your table looks like this :
CREATE TABLE htmlComponent
(
id int auto_increment primary key,
action int,
button_id int not null,
type varchar(20),
dtInserted datetime,
dtUpdated datetime
);
CREATE UNIQUE INDEX buttonType ON htmlComponent(button_id, type);
Now we need to update the table according to the buttons / atributes you have for a specific action.
-- Reset dtInserted and dtUpdated for action 1
UPDATE htmlComponent SET dtInserted = null, dtUpdated = null WHERE action=1;
-- INSERT or UPDATE according to the data inside the json structure
INSERT INTO htmlComponent (action, button_id, type, dtInserted)
VALUES
(1, 1, 'button', NOW()),
(1, 3, 'attribute', NOW())
ON DUPLICATE KEY UPDATE
button_id = VALUES(button_id),
type = VALUES(type),
dtInserted = null,
dtUpdated = NOW();
-- Getting the result
SELECT * FROM htmlComponent where action=1;
Your should end up with this result which will make it easy to figure out what doesn't exists anymore, what is new and what was updated.
+----+--------+-----------+-----------+----------------------------+----------------------------+
| ID | ACTION | BUTTON_ID | TYPE | DTINSERTED | DTUPDATED |
+----+--------+-----------+-----------+----------------------------+----------------------------+
| 1 | 1 | 1 | button | (null) | February, 09 2015 16:21:49 |
| 3 | 1 | 2 | attribute | (null) | (null) |
| 4 | 1 | 3 | attribute | February, 09 2015 16:21:49 | (null) |
+----+--------+-----------+-----------+----------------------------+----------------------------+
Here is a fiddle. Please note I had to put the UPDATE and the INSERT in the left panel because DML are not allowed in the query panel.

Creating a stored procedure that uses (IF ELSE) to decide what goes in a new column

I'm trying to create a stored procedure to show specific information that I need, however I don't have a column that says Yes/No when taking my dates into account.
Example...
I have a table with this info
| ID | Name | DOB |
| 1 | John | 1/1/1991 |
What I want to see is
| ID | Name | DOB | Under 21 |
| 1 | John | 1/1/1991 | No |
I'm thinking that I need to me a temp table or something in order to incorporate that extra column, then use an if/else statement to decide on whether the DOB is greater than said date.
Does anyone have any pointers that they could help me with?
Much appreciated
you can try using a case like this
SELECT *, CASE WHEN DATEDIFF(year,DOB,GETDATE()) < 21 THEN 'No'
ELSE 'Yes'
END AS 'Under 21'
FROM Table_Name

Running a quartz job in grails?

I'm very new to Grails. I have a table like this :
+----+---------+----------------+----------------+-------------+--------------------+--------------+--------+---------------------+
| id | version | card_exp_month | card_exp_year | card_number | card_security_code | name_on_card | txn_id | date_created |
+----+---------+----------------+----------------+-------------+--------------------+--------------+--------+---------------------+
| 9 | 0 | ASdsadsd | Asdsadsadasdas | Asdsa | | batman | asd | 2012-08-13 19:38:22 |
+----+---------+----------------+----------------+-------------+--------------------+--------------+--------+---------------------+
In mysql. I wish to run a Quartz job against this table, which will compare, date_created Time stamp with present time such that, if any field's there with timestamp less than 30 minutes should be deleted.
How can I do this?
you could define a Job implementing your logic ( in the execute() method, check (date_created - now) < 30 minutes or else delete the row in the database) and then trigger this job on a regular basis.
You can read the documentation http://quartz-scheduler.org/documentation/quartz-2.1.x/cookbook or have a look at the examples : http://svn.terracotta.org/svn/quartz/branches/quartz-2.2.x/examples/src/main/java/org/quartz/examples/example1/
Check this example for grails quartz:
http://www.juancarlosfernandez.net/2012/02/como-crear-un-proceso-quartz-en-grails.html