mysql - insert multiple values for single column - mysql

I want to insert data into MySQL with multiple values in one field and remaining fields contains only one value.
I have tried below query
INSERT INTO assessment_training(
PARENT_SLNO
RNO
TRAINING_CATEGORY
TRAINING_NAME
)
VALUES (
9,
1,
'Technical',
(1,7)
);
Error #1241
`INSERT INTO assessment_training(PARENT_SLNO,RNO,TRAINING_CATEGORY,TRAINING_NAME) VALUES (9,1,'Technical',(1,7)) Error Code: 1241. Operand should contain 1 column(s)`

You can use CONCAT()
INSERT INTO assessment_training(
PARENT_SLNO,
RNO,
TRAINING_CATEGORY,
TRAINING_NAME
)
VALUES (
9,
1,
'Technical',
CONCAT("1", ",", "7")
);

Related

Get top x values in mysql table according to column value

I have the following table data:
content,count
'a', '1'
'b', '2'
'c', '2'
'd', '2'
'content_name', '18'
'de', '2'
'dea', '2'
'deaasdfdsaf', '5'
'content_name', '17'
I would like to have the correct query for getting x (for example 4) rows with the biggest values in their content column
I have tried the answers given in:
Getting Top 3 from Mysql table based on Condition/Value
but it didnt work as i was expecting
it keeps giving me just the rows by their table location
any suggestions?
create table Test(content varchar(100),count integer );
insert into Test(content, count) values("a", 1);
insert into Test(content, count) values("b", 2);
insert into Test(content, count) values("c", 2);
insert into Test(content, count) values("d", 2);
insert into Test(content, count) values("content_name", 18);
insert into Test(content, count) values("de", 2);
insert into Test(content, count) values("dea", 2);
insert into Test(content, count) values("deaasdfdsaf", 5);
insert into Test(content, count) values("content_name", 7);
select * from Test
order by count desc
limit 4;
However, I am not quite sure what you mean by "biggest values in their content column", therefore I ranked by their count values.
If you are actually looking for the number of character within the content column, which was my initial impression reading your question:
Using Martin Stagl's post to create the example table .
select *, LENGTH(REPLACE(content, 'N', '')) as char_length from Test;
NOTE: If this is not working on the server, try replacing the LENGTH() function with LEN()
Fiddle: http://sqlfiddle.com/#!9/032d48/9

Coalesce not working in insert statement for a null table

I am trying to use the coalesce function in SQL to avoid getting an error when inserting a row with an auto-incrementing uid into a table that is null. However, the following code is still giving me:
"cannot insert the value null into column 'TABLE_ONE_UID'".
cmdEx.ExecuteNonQuery(
"INSERT INTO TABLE_ONE
(TABLE_ONE_UID, USER_UID, SHT_DATE,
C_S_UID, CST_DATE,
CET_DATE, S_M, PGS)
VALUES ((SELECT MAX(COALESCE(TABLE_ONE_UID, 0)) + 1
FROM TABLE_ONE),
127, '2009-06-15T13:45:30',
0, '2009-06-15T13:45:30','2010-06-15T13:45:30',
'TEST DELETE THIS ROW', 0 )");
The correct way to solve this is with an auto_increment column:
create table PMS_CALC_SCHEDULE (
PMS_CALC_SCHEDULE_UID int auto_increment primary key,
. . .
);
If, for some reason, you want to do the calculation yourself, subject your code to race conditions, and have slower inserts, then you need to do the coalesce in the right place:
INSERT INTO PMS_CALC_SCHEDULE (PMS_CALC_SCHEDULE_UID, . . .)
SELECT COALESCE(MAX(PMS_CALC_SCHEDULE_UID), 0) + 1,
. . .
FROM PMS_CALC_SCHEDULE ;
This would happen when your source table doesn't have any row, MAX would return null in this case.
To prevent this, you can use interchange COALESCE and MAX, e.g.:
INSERT INTO PMS_CALC_SCHEDULE
(PMS_CALC_SCHEDULE_UID, USER_UID, SCHEDULED_DATE,
PMS_CALC_STATUS_UID, CALCULATION_START_DATE,
CALCULATION_END_DATE, STATUS_MESSAGE, PROGRESS)
VALUES ((SELECT COALESCE(MAX(PMS_CALC_SCHEDULE_UID), 0) + 1
FROM PMS_CALC_SCHEDULE),
127, '2009-06-15T13:45:30',
0, '2009-06-15T13:45:30','2010-06-15T13:45:30',
'TEST DELETE THIS ROW', 0 )")
Here's the SQL Fiddle.
If the field is set to AutoIncrement on the table itself, just leave the field out of your Insert-Statement. The value is added by the database itself.

MySQL #1241 - Operand should contain 1 column(s)

I want to update the column 'page_inv', so it will replace two last chars of this column, and add new data, for example:
'{"userItems":[{item1}]}' -> '{"userItems":[{item1}' -> '{"userItems":[{item1}, {item2}]}'
UPDATE `users`
SET `page_inv`=CONCAT((SELECT `page_inv`, SUBSTRING( `page_inv`, 1, CHAR_LENGTH( `page_inv` )) -2), ',{newItem}]}')
WHERE `STEAMID`=76561198147
My DB:
Something like this?
UPDATE `users`
SET page_inv= CONCAT(left(page_inv, length(page_inv) - 2), ',{newItem}]}')
WHERE STEAMID = 76561198147;

Subquery with MySql using INSERT

I'm trying to update the database, using a script where the ID of a user isn't readily known, so I'm using a subquery to have mysql find the user id (for the posteruserid value). This is the SQL query i'm using:
INSERT INTO `thread` (`title`, `forumid`, `open`, `replycount`,
`postercount`, `postusername`, `postuserid`, `lastposter`,
`dateline`, `visible`, `keywords`)
SELECT 'IN', 2, 1, 0, 1, 'lemons', `userid` FROM `user`
WHERE `username` = 'lemons', 'lemons', 1375768440, 1, 'IN';
I'm getting a syntax error from the above SQL, and I can't figure out what I'm doing wrong.
EDIT because of the mismatched column name, I tried using an alias, which still doesn't work
INSERT INTO `thread` (`title`, `forumid`, `open`, `replycount`,
`postercount`, `postusername`, `postuserid`, `lastposter`,
`dateline`, `visible`, `keywords`)
SELECT 'IN', 2, 1, 0, 1, 'lemons',
`userid` AS `postuserid` FROM `user` WHERE `username` = 'lemons',
'lemons', 1375768440, 1, 'IN';
column mismatch in insert and select query..column should be same where you are going to insert and from where you are fetching data.
You specify to insert values of 11 columns, but in your SELECT statement, you are providing only 7 values. Please provide the value for lastposter,dateline,visible, and keywords.

What am I doing wrong with my query syntax?

I am trying to insert a row into my table with the following query in ssms:
INSERT INTO [Reservation].[dbo].[Contacts]
([ContactID]
,[Name]
,[Phone]
,[Email]
,[QoowayUserName])
VALUES
(<"100", nvarchar(50),>
,<"Vincent Chase", nvarchar(50),>
,<"3103331234", nvarchar(50),>
,<"vincent_chase#hollywood.com", nvarchar(50),>
,<"Username", nvarchar(50),>)
GO
I get the error:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near '<'.
I've also tried using single quotes. What am I doing wrong?
INSERT INTO Reservation.dbo.Contacts
(ContactID
,Name
,Phone
,Email
,QoowayUserName)
VALUES
('100'
,'Vincent Chase'
,'3103331234'
,'vincent_chase#hollywood.com'
,'Username')
You might need to remove the "dbo", like this:
INSERT INTO Reservation.Contacts
(ContactID
,Name
,Phone
,Email
,QoowayUserName)
VALUES
('100'
,'Vincent Chase'
,'3103331234'
,'vincent_chase#hollywood.com'
,'Username')