need to adapt script for mysql - mysql

USE ActivityLog;
GO
SETUSER 'dbo'
GO
IF NOT EXISTS (SELECT 1 FROM Events WHERE EventID = 282 AND EV_Event = 'Report Imported')
BEGIN
INSERT INTO dbo.Events
(EventID, CategoryID, EV_Event, EV_Template, EV_IsActive, EV_CreationDate)
VALUES
(282, 6, 'Report Imported', '', 1, GETDATE())
END
GO
I want to edit above mentioned script for mysql.

I had to remove GO and IF NOT EXIST, GETDATE() is also changed:
INSERT INTO dbo.Events
(EventID, CategoryID, EV_Event, EV_Template, EV_IsActive, EV_CreationDate, EV_DmlDate, EV_DmlUserID)
VALUES
(282, 6, 'Reconciliation Report Imported', '', 1, CURDATE(), CURDATE(), 0)
Thanks!

Related

how to insert specific data on specific id in sql

i have 2 query first one is
SELECT stdntCode
FROM dbo.CourseStudent
WHERE(Status = 'Active') AND (CrsID = 1) AND (GrpID = 8)
second one is insert
INSERT INTO StudentExam (ExamID, CrsID, stdntCode, [Degree], StudGrpID, IsAttend, Notes, dateexam, [date])
VALUES(4, 3, stdntCode, 8.0, 1, 1, N'', '2023-01-17', '2023-02-01');
i want to insert this data to my first query i want replace stdntCode in my insert query with stdntCode list from my first query
thank you
i try that query
insert into StudentExam (stdntCode ,ExamID, CrsID, [Degree], StudGrpID, IsAttend, Notes, dateexam, [date])
select (SELECT stdntCode
FROM dbo.CourseStudent
WHERE(Status = 'Active') AND (CrsID = 1) AND (GrpID = 8)) as IDClient,
(4, 3,0, 1, 1, N'', '2023-01-17', '2023-02-01');
You need to provide the column values as part of a single select statement, like the following:
insert into StudentExam (stdntCode ,ExamID, CrsID, [Degree], StudGrpID, IsAttend, Notes, dateexam, [date])
select stdntCode, 4, 3, 0, 1, 1, N'', '2023-01-17', '2023-02-01'
from dbo.CourseStudent
where Status = 'Active' and CrsID = 1 and GrpID = 8;

Insert multiple rows from select result

For a database fix I have to perform I need to do an insert for every ID this query results;
SELECT Id FROM role WHERE id != 99 AND Id NOT IN (SELECT RoleId FROM permissionaction WHERE PermissionId = 19);
So it results some ID's which have to be added with this query.
INSERT INTO permissionaction (AccessLevel, PermissionId, ControllerActionId, Active, CreatedOn, ModifiedOn, CreatedById, ModifiedById, RoleId) VALUES (1, 19, 1, 1, Now(), Now(), 1, 1, *******ID HERE******);
I know it can be done with using mysql cursors but it's not a possibility here. Is there a way to add all of the resulting ID's in one query?
Thanks
Use INSERT...SELECT:
INSERT INTO permissionaction (AccessLevel, PermissionId, ControllerActionId, Active, CreatedOn, ModifiedOn, CreatedById, ModifiedById, RoleId)
SELECT 1, 19, 1, 1, Now(), Now(), 1, 1, Id
FROM role
WHERE id != 99
AND Id NOT IN (SELECT RoleId FROM permissionaction WHERE PermissionId = 19);
With INSERT...SELECT you can add values to the table permissionaction for each returned row of the SELECT query.

Not have Subquery,But the mysql throw Subquery return more than 1 row

A standard insert MySQL query without any Subquery, but the MySQL tells me
Subquery return more than 1 row
INSERT INTO
db_novelV2.tbl_saler_todo
(
is_drive, customer_type, operator_id, STATUS, remark,
update_dt, receive_saler, src_id, come_dt, has_give_present,
TYPE, service_id, src_type, allot_saler, has_car_shuttle,
customer_id, session_id, arrive_dt, creater_id, leave_dt,
collector_id, store_id, allot_dt, add_dt
)
VALUES
(0, 0, 528, 0, '',NULL, 307,0, NOW(), 0, 1, 352,0,307, 0, 243465,2993333,NOW(), 528, NULL, 0,4, NOW(), NOW());
Check whether you have any INSERT triggers on your table, If so, disable any triggers before running your INSERT statement.

mysql: write query with if in where part

i have this table:
(`id`, `name`, `type`, `price`)
(1, 'cats', 1, 12.25),
(2, 'dogs', 0, 11.35),
(3, 'house', 1, 7.25),
(4, 'cats2', 2, 5.26);
I need select all data, but if type is 1, i need get items witch price is more than 10.
I create this query:
SELECT *
FROM `items`
WHERE IF(TYPE = "1", price > 10, 1 = 1)
Works well, but maybe possible write much smarter or in other way?
Maybe don't need "1 = 1"?.
I would like to know your advice, thanks
Your 1=1 is senseless, but your IF is not. You can use just 1:
SELECT *
FROM `items`
WHERE IF(TYPE = "1", price > 10, 1)
-since MySQL evaluates expression as bool (actually, int) - and so 1 means 'true'.
But on the other hand, there's logic equivalent for your condition:
SELECT *
FROM `items`
WHERE `price`>10 OR `type`!="1"
However, I've faced such case in another question and, after some researching, I've discovered that IF is faster, even if it looks more complicated.

Increase Alphanumeric VARCHAR Entry by Value 1?

On an old project because of not thought through design I have a column which actually should be set to auto_increment, though it cannot be because it are alphanumeric entries as follows:
c01
c02
c03
(c99 would continue to c100 and more), the letter happened in the past and it would require to overhaul the system to take it out, thus I rather prefer this workaround.
Now I need a way to imitate the auto_increment functionality with the SQL statement myself, my own attempt has gotten as far as the following:
INSERT INTO tags (tag_id, tag_name, tag_description, added_by_user_id, creation_date, last_edited) VALUES (SELECT(MAX(tag_id)+1),
'Love', 'All about love', 7, now(), 0);
This one does not work as is, though the idea was to select the highest entry in the column "tag_id" and then simply increase it by the value 1.
Any ideas how to accomplish this?
By the way I am also not sure if you simply can increase an alphanumeric entry through this way, though I know it can be done, I just don't know how.
If you want to safely get the largest integer value of a tag id of the form c##.., you could use the following expression:
max( convert( substring(tag_id, 2) , unsigned integer) )
^^^ largest ^^^^^^^^^ after 'c' ^^^^^^^^^^^^^^^^ convert to positive number
Then your insert statement would look something like this:
set #newid = convert(
(select
max(convert( (substring(tag_id, 2)) , unsigned integer))+1
from tags), char(10)
);
set #newid = if(length(#newid) = 1, concat('0', #newid), #newid);
set #newid = concat('c', #newid);
INSERT INTO tags (tag_id, tag_name, tag_description, added_by_user_id,
creation_date, last_edited)
VALUES (#newid, 'Love', 'All about love', 7, now(), '2012-04-15');
Demo: http://www.sqlfiddle.com/#!2/0bd9f/1
this will increase from c01 to c02 to c03 ... to c99 to c100 to c101 ... to c999 to c1000 etc.
set #nextID = (SELECT CONCAT(SUBSTRING(`tag_id`, 1, 1), IF(CHAR_LENGTH(CAST(SUBSTRING(`tag_id`, 2)
AS UNSIGNED)) < 2, LPAD(CAST(CAST(SUBSTRING(`tag_id`, 2) AS UNSIGNED) + 1 AS CHAR), 2,
'0'), CAST(CAST(SUBSTRING(`tag_id`, 2) AS UNSIGNED) + 1 AS CHAR))) FROM `tags` ORDER BY
`tag_id` DESC LIMIT 1);
INSERT INTO tags (tag_id, tag_name, tag_description, added_by_user_id,
creation_date, last_edited) VALUES (#nextID, 'Love', 'All about love', 7, NOW(), null);