Data is not inserted in mysql - 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)

Related

Get data of table with some data from other table

I have a simple table visitor and another table visitor_tokens.
SQL creation script of visitor:
CREATE TABLE `visitor` (
`id` int(7) UNSIGNED NOT NULL,
`phone` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ALTER TABLE `visitor`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `phone_index` (`phone`);
ALTER TABLE `visitor`
MODIFY `id` int UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
INSERT INTO `visitor` (`phone`) VALUES
('111111111');
SQL creation script of visitor_tokens:
CREATE TABLE `visitor_tokens` (
`id` int(7) UNSIGNED NOT NULL,
`visitor` int(7) UNSIGNED NOT NULL,
`token` varchar(100) COLLATE utf8mb4_general_ci NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE `visitor_tokens`
ADD PRIMARY KEY (`id`);
ALTER TABLE `visitor_tokens`
MODIFY `id` int UNSIGNED NOT NULL AUTO_INCREMENT;
INSERT INTO `visitor_tokens` (`visitor`, `token`) VALUES
(1, 'abc_token'),
(1, 'xyz_token');
I want to get some data of visitor by id:
I want to get the phone number (phone column) and the visitor tokens (visitor_tokens.token). All - according to given id.
My current SQL script is: SELECT visitor.phone, visitor_tokens.token FROM visitor JOIN visitor_tokens ON visitor_tokens.visitor=visitor.id WHERE id=1. This gives me only the phone and the first token: abc. But I also want to get the tokens of the visitor from the second table. To get something like [abc_token, xyz_token]. How can I do it?
So this is my solution (also recommend on reading the main comments):
SELECT visitor.phone, GROUP_CONCAT(visitor_tokens.token) AS tokens FROM visitor LEFT JOIN visitor_tokens ON visitor_tokens.visitor=visitor.id WHERE visitor.id=1 LIMIT 1;
It returns result regardless of missing visitor tokens (LEFT JOIN), and also if there are tokens - it returns the tokens separated by comma in one row.

How can I insert or update if a value is different?

I'm trying to write an SQL query that will set a value to 1 when it updates another column.
Specifically, I want to set my is_patch_file to 1 if the corresponding hash column is changed. If the hash column is NOT changed, it should keep whatever the currently stored is_patch_file value is.
Is this possible?
Table Structure
DROP TABLE IF EXISTS `program_files`;
CREATE TABLE `program_files` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`folder_id` int(10) unsigned NOT NULL,
`name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`path` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`hash` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`enabled` tinyint(1) NOT NULL DEFAULT '1',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`is_patch_file` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `unique_file` (`folder_id`,`name`,`path`,`hash`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL Queries
INSERT INTO program_files (folder_id, name, path, hash, is_patch_file, enabled, created_at, updated_at) VALUES (3, 'TestName', 'TestPath', 'TestHash', 0, 1, NOW(), NOW()) ON DUPLICATE KEY UPDATE
is_patch_file=1, #Is Patch file should ONLY be 1 if the hash is different from the original hash...otherwise keep our current value how do I do this?
hash=VALUES(hash),
updated_at=VALUES(updated_at);
INSERT INTO program_files (folder_id, name, path, hash, is_patch_file, enabled, created_at, updated_at) VALUES (3, 'TestName', 'TestPath', 'TestHash2', 0, 1, NOW(), NOW()) ON DUPLICATE KEY UPDATE
is_patch_file=1, #Is Patch file should ONLY be 1 if the hash is different from the original hash...otherwise keep our current value how do I do this?
hash=VALUES(hash),
updated_at=VALUES(updated_at);
You can compare hash to VALUES(hash) before it gets updated, and use that when setting is_patch_file.
ON DUPLICATE KEY UPDATE
is_patch_file = hash != VALUES(hash),
hash = VALUES(hash),
updated_at = VALUES(updated_at)

Keeping last seen values in a summary table

I have two tables:
parameters keeps all the para_ids and their names and is always updated to have all parameters in it.
CREATE TABLE `parameters` (
`para_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`para_id`),
UNIQUE KEY `idx_parameters_name` (`name`)
) ENGINE=InnoDB;
processing is holding a chunk of data every 5 minutes.
CREATE TABLE `processing` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`t_ns` bigint(20) unsigned NOT NULL DEFAULT '0',
`para_id` int(10) unsigned NOT NULL DEFAULT '0',
`value` varchar(1024) NOT NULL DEFAULT '',
`isanchor` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `data` (`para_id`,`t_ns`)
) ENGINE=InnoDB;
I want to keep a table actual_values with the last seen values that each parameter (if it occurred in processing) had. The para_ids are updated with an INSERT IGNORE before the update. Currently I have those queries:
INSERT IGNORE INTO actual_values (para_id) (SELECT DISTINCT para_id FROM parameters);
UPDATE actual_values a
JOIN processing p ON a.para_id = p.para_id
SET a.value = (SELECT p.value FROM processing p WHERE a.para_id = p.para_id ORDER BY t_ns DESC LIMIT 1);
I feel like this is not the optimal way to go, it takes quite long. Do you guys have better suggestions?

how to write create table date query in phpmyadmin which store by default current date if date not given?

I am trying to create a table (phpMyAdmin) by using the following query:
CREATE TABLE login_detail(
Id int(11) primary key auto_increment,
userName varchar(100) not null,
userPassword varchar(100) not null,
created_at Date DEFAULT CURRENT_DATE
);
but it showing error at CURRENT_DATE. Can anyone solve this problem?
Its not supported.
The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column
http://dev.mysql.com/doc/refman/5.5/en/create-table.html
you just use current time stamp .below code i tested in phpmyadmin.it working fine
CREATE TABLE login_detail(Id int(11) primary key auto_increment, userName varchar(100) not null,userPassword varchar(100) not null, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Try the following:
CREATE TABLE IF NOT EXISTS `login_detail` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(100) NOT NULL,
`userPassword` varchar(100) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
INSERT INTO `login_detail` (`Id`, `userName`, `userPassword`) VALUES
(1, 'aaa', 'bbb'),
(2, 'aaa', 'bbb');
To get the date and-or time in the format you want use DATE_FORMAT
SELECT `Id`, `userName`, `userPassword`, DATE_FORMAT(`created_at`, '%e %b %Y') AS `created_at_date`, DATE_FORMAT(`created_at`, '%H:%i:%s') AS `created_at_time` FROM `login_detail`;
http://sqlfiddle.com/#!9/ea673/1

Nested sql insert from php

I have a nested array in php and I'm trying to insert data into a mysql table only if a duplicate does not exist. The duplicate should only check match against the sensor_serial and dates field. I'm having no luck with DUPLICATE as the id is unique regardless of the other fields. this is the table layout:
CREATE TABLE IF NOT EXISTS `temps_test` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`sensor_serial` varchar(64) COLLATE latin1_general_ci DEFAULT NULL,
`temp_c` float DEFAULT NULL,
`dates` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `date` (`dates`)
)
This is my non working code. I can't seem to get it quite right.
foreach ($output as $k => &$v) {
$sql = "INSERT INTO temps_test (sensor_serial,temp_c,dates) VALUES ('$v[7]','$v[3]','$v[5]') WHERE NOT EXISTS (SELECT * FROM temps_test WHERE sensor_serial='$v[7]' AND dates='$v[5]')";
$go = mysql_query($sql) or die( mysql_error() );
}
Can this be done this way or is my approach totally off?
Try using ON DUPLICATE KEY UPDATE
INSERT INTO temps_test (sensor_serial,temp_c,dates)
VALUES ('val1','val2','val3')
ON DUPLICATE KEY UPDATE sensor_serial=sensor_serial;
make sure to add constraint on field sensor_serial, example
CREATE TABLE IF NOT EXISTS `temps_test`
(
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`sensor_serial` varchar(64) COLLATE latin1_general_ci DEFAULT NULL,
`temp_c` float DEFAULT NULL,
`dates` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `date` (`dates`),
CONSTRAINT t_uq INIQUE (`sensor_serial`) -- <== this one
)