how to insert specific data on specific id in sql - mysql

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;

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

need to adapt script for 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!

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.

Special sorting for MySQL result

Is it possible to get MySQL to put a row which matches some condition on the first position?
For instance, say that my result is [1, 3, 5, 6, 7, 1987] and I want it to become [6, 1, 3, 5, 7, 1987] (I want the number 6 to be on the first position)
I feel you are searching for this
SELECT * FROM `pctable`order by colName=6 desc, colName
so colName = 6 will be in top and then for others it will be id wise sort.
Try to use CASE expression in your Order by clause. Such as
ORDER BY CASE WHEN COL_VALUE = 'xxx' THEN '1'
WHEN COL_VALUE = 'xxx' THEN '2'
ELSE COL_VALUE END ASC;
Or with IF in the SELECT somehow:
SELECT IF(number = 6, number, ''), number FROM colname

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.