Update a variable in a mysql table depending on a time difference - mysql

CONTEXT
I would like to update a variable inside a table depending on a time difference.
DATA
I have the following mySQL Table
CREATE TABLE `userTP` (
`id` int(11) NOT NULL,
`profile` int(11) NOT NULL DEFAULT '1',
`username` varchar(50) NOT NULL,
`inprogress` int(11) NOT NULL,
`launchedat` datetime DEFAULT NULL,
`password` varchar(255) NOT NULL,
`npoints` int(11) NOT NULL DEFAULT '100',
`ms` int(11) NOT NULL DEFAULT '5000'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `userTP` (`id`, `profile`, `username`, `inprogress`, `launchedat`, `password`, `npoints`, `ms`) VALUES
(4, 0, 'binome1', 1, '2020-11-10 17:20:07', 'vvvvv', 10, 1000),
(48, 0, 'binome2', 0, '2020-06-11 14:45:07', 'llll', 10, 1000);
ALTER TABLE `userTP`
ADD PRIMARY KEY (`id`);
ALTER TABLE `userTP`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=49;
COMMIT;
WHAT I WOULD LIKE TO DO
If the time difference between launchedat and the current time is larger than 10 minutes, I would like to update the variable inprogress and set it to 2 for user with id 4
WHAT I TRIED
UPDATE userTP SET inprogress = '2' WHERE ROUND(TIME_TO_SEC(timediff(launchedat,NOW()))/60)>10 AND id = 4
This gives no error but does not work .....
Any Idea ?

Related

mysql insert into select join - copy values from one column to another table, passing through a connecting table

I can't get this to work
CREATE TABLE `oc_tax_class` (
`tax_class_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `oc_tax_rate`
--
CREATE TABLE `oc_tax_rate` (
`tax_rate_id` int(11) NOT NULL,
`geo_zone_id` int(11) NOT NULL DEFAULT 0,
`name` varchar(255) NOT NULL,
`rate` decimal(15,4) NOT NULL DEFAULT 0.0000,
`type` char(1) NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `oc_tax_rule`
--
CREATE TABLE `oc_tax_rule` (
`tax_rule_id` int(11) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`tax_rate_id` int(11) NOT NULL,
`based` varchar(10) NOT NULL,
`priority` int(5) NOT NULL DEFAULT 1
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
3 tables. I want oc_tax_class.title = oc_tax_rate.name
I believe, although I'm not sure, that I should
INSERT INTO oc_tax_class(title)
or
UPDATE oc_tax_class SET title = ...
SELECT oc_tax_rate.name, oc_tax_rule.tax_class_id
JOIN oc_tax_rule ON oc_tax_rate.tax_rate_id = oc_tax_rule.tax_rate_id
And then I don't know what to do next.
I need to copy values from one column to another table, passing through a connecting table.
MySQL supports a multi-table UPDATE syntax, but the documentation (https://dev.mysql.com/doc/refman/en/update.html) has pretty sparse examples of it.
In your case, this may work:
UPDATE oc_tax_class
JOIN oc_tax_rule USING (tax_class_id)
JOIN oc_tax_rate USING (tax_rate_id)
SET oc_tax_class.title = oc_tax_rate.name;
I did not test this. I suggest you test it first on a sample of your data, to make sure it works the way you want it to.

How do I INSERT into a database with a primary autoincrement key?

So I have a table that is laid out according to:
CREATE TABLE `dealerships` (
`dealership_id` BIGINT(20) UNSIGNED NOT NULL,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
Now when I insert with
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES( NULL, ~values);
This returns an error that dealership_id cannot be NULL error 1048.
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES(0, ~values);
This returns an error that dealership_id already exists for value 0.
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES(default, ~values);
This returns an error that dealership_id has no default value error 1364.
When I simply say screw it, and ommit dealership_id
INSERT INTO dealerships (~the rest~), VALUES(~values);
I get the error that a value must be specified for dealership_id.
Every-time I've worked with autoincrement primary keys. Normally inserting NULL would work for the index to do the auto-magical things for me. What is happening??
Platform Specific Information:
MariaDB10.1.18 x64
Windows Server 2012 R2
For create add AUTO_INCREMENT
CREATE TABLE `dealerships` (
`dealership_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
and for insert don't insert the key (is automatically create by auto increment)
insert into `dealerships` (
`zone`,
`phone`,
`fax` ,
`name`,
`address1`,
`address2`',
`servicephone` ,
`timezone`
) values (
'vale_zone',
1,
2 ,
'val_name',
'val_address1',
'val_address2',
3 ,
'val_timezone'
)
To insert a field with auto increment property, use ID int NOT NULL AUTO_INCREMENT in the create table.
CREATE TABLE `dealerships` (
`dealership_id` INT NOT NULL AUTO_INCREMENT,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
After, you have two solutions to insert data.
Solution 1
INSERT INTO dealerships(zone, phone, fax, name) VALUES('zone1', '00000', '00000', 'name1')
In this case I chossen to add data in only few columns (add the others on the wame way).
Solution 2
INSERT INTO dealerships SET zone = 'zone1', phone = '0000', name = 'Eddy'
Here, I used the field SET field = value, field2 = value2.
As you can see I didn't added any reference to the ID. Normal, the field is AUTO_INCREMENT mode, and Mysql (or tothers SGBD) will create automatically a value.
Of course, you can force the value:
INSERT INTO dealerships SET dealership_id = 10, zone = 'zone1', phone = '0000', name = 'Eddy'
INSERT INTO dealerships(dealership_id, zone, phone, fax, name) VALUES(9, 'zone1', '00000', '00000', 'name1')
http://www.w3schools.com/sql/sql_insert.asp
About your errors
Normal. You specified NOT NULL
Normal, there is already a record for the value 0 (check your database)
Normal. You can't use default. This is a constant with his own value (1364).
Normal, the field as if can't be ommited
The only way to does not have this issue is to remove completely the reference of the ID field. Use the solution described :)
Georges explained it all for you. If you want to add auto increment to table just run this sql statement
ALTER TABLE dealerships MODIFY COLUMN dealership_id BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT;
This will add the auto increment property on the column dealership_id. And if you want to change auto increment start value run this (Change 100 to your start value)
ALTER TABLE dealerships AUTO_INCREMENT=100;

selecting multiple rows issue in mysql

I am beginner in sql.
I have two tables users and installments
CREATE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(20) NOT NULL,
`password` varchar(10) NOT NULL,
`father_name` varchar(20) NOT NULL,
`phone` varchar(20) NOT NULL,
`cnic` varchar(20) NOT NULL,
`email` varchar(100) NOT NULL,
`address` varchar(100) NOT NULL,
`introducer` varchar(100) NOT NULL,
`date` date DEFAULT NULL,
`reg_number` varchar(100) DEFAULT NULL,
`installment` int(100) NOT NULL,
`user_level` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `cnic` (`cnic`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
INSERT INTO `users` (`id`, `user_name`, `password`, `father_name`, `phone`, `cnic`, `email`, `address`, `introducer`, `date`, `reg_number`, `installment`, `user_level`) VALUES
(2, 'qaser', 'Qaser1', 'zamarrud', '0312546879', '37406-3140185-1', 'tariq_kareem#yahoo.com', 'street # 6', 'rizwan', '2014-08-20', 'E-002', 3000, 0);
and
CREATE `installments` (
`installment_id` int(11) NOT NULL AUTO_INCREMENT,
`month` date DEFAULT NULL,
`prv_arrear` int(100) NOT NULL,
`amount` int(100) NOT NULL,
`total` int(100) NOT NULL,
`receive` int(100) NOT NULL,
`arrear` int(100) NOT NULL,
`fk_users_id` int(11) NOT NULL,
PRIMARY KEY (`installment_id`),
KEY `fk_users_id` (`fk_users_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
INSERT INTO `installments` (`installment_id`, `month`, `prv_arrear`, `amount`, `total`, `receive`, `arrear`) VALUES
(2, '2014-08-20', 2000, 2500, 4500, 3000, 1500);
when I run the following query I get the first table's record correctly but the second table's record showing NULL values
SELECT * FROM users
LEFT JOIN installments
ON users.id=installments.installment_id
WHERE users.cnic='37406-3140185-1';
Is there anything missing in the above query or is there another way to get the record from both tables simultaneously
id user_name password father_name phone cnic email address introducer date reg_number installment user_level installment_id month prv_arrear amount total receive arrear fk_users_id
2 qaser Qaser1 zamarrud 0312546879 37406-3140185-1 tariq_kareem#yahoo.com street # 6 rizwan 2014-08-20 s-001 3000 0 NULL NULL NULL NULL NULL NULL NULL NULL
I am also using the following query to inserting the record for getting primary key value and insert into foreign key
INSERT INTO `installments`(`id`, `month`, `prv_arrear`, `amount`, `total`, `receive`, `arrear`, fk_users_id)
SELECT NULL,now(),1000,2500,3500,3000,500, id
FROM users
WHERE cnic = '37406-3140190-1'
please help me thanks in advance and sorry if there is something wrong in my question because I am new in sql.
I suspect the right join condition is on fk_users_id. So this might do what you want:
SELECT *
FROM users u LEFT JOIN
installments i
ON u.id = i.fk_users_id
WHERE u.cnic = '37406-3140185-1';

Data is not inserted in mysql

I have the following table:
(Yes, the table names are silly... I'm just messing about)
CREATE TABLE `habitat`.`habit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
When I try to do the following sql statement, only 0 shows up in content:
INSERT into habit
(content, user_id)
VALUES (content = 'this is some habit', user_id = 2)
Basically you do not need to include the "columnName = value" in the VALUES portion of an insert statement. It should look like this.
INSERT INTO habit (
content,
user_id)
VALUES (
'this is some habit',
2)

MySQL table incrementing by 10 for some reason

The id field in a mysql table is incrementing by 10 (11, 21, 31) for some reason. Here is the table definition:
CREATE TABLE `clients` (
`id` int(11) NOT NULL auto_increment,
`first_name` varchar(255) default NULL,
`last_name` varchar(255) default NULL,
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;
If I do a simple insert statement in SQL the next ID will be 41.
You have auto_increment_increment set to 10, change it back to 1.