mysql select * from not in a table - mysql

select * from csclass where cnumber not in (
(select distinct cnumber from temp where taken=0) union (select cnumber from taken where username = "1")
);
This is what i have now. I just google it and realized that not in could not be a table. So how could i do this?
Thank you.
The below is my whole code.
create temporary table temp
(cnumber VARCHAR(45) not null
, lclass VARCHAR(45) not null
,taken boolean not null default false
);
insert into temp(cnumber,lclass)
select uclass,lclass from pre;
update temp,taken set temp.taken=true where temp.lclass=taken.cnumber;
select * from csclass where cnumber not in (
(select distinct cnumber from temp where taken=0) union(select cnumber from taken where username = "1")
);

So sorry for my bad English skill. So when i run this, there is error message: Error Code 1064. Syntax error.
So in the end i fixed this with below code:
select * from csclass where cnumber not in ( select cnumber from(
(select distinct cnumber from temp where taken=0) union (select cnumber from taken where username = "1")
) as total) ;
And Thank you guys.

Related

MySQL error - #1242 - Subquery returns more than 1 row

I have been trying to update values in a MySQL table and I got this error from running the code below. Help.
UPDATE overall
SET Total_Score = (
SELECT (
ot.Total_Score + op.Total_Score
) AS Total_Score
FROM (
SELECT StudentID, Total_Score
FROM term2
GROUP BY StudentID
) AS ot
INNER JOIN (
SELECT StudentID, Total_Score
FROM overall
GROUP BY StudentID
) AS op ON ( op.StudentID = ot.StudentID )
)

Operand Should Contain 1 Column(s), Trying to generate volunteer data

I can't seem to troubleshoot my problem.
My stored procedure:
CREATE DEFINER=`myschoolusername`#`%` PROCEDURE `generate_volunteers`(in nfolks int)
BEGIN
set #i=0;
while #i < nfolks do
insert into Volunteer(firstname, lastname, dateofbirth)
values (((floor(1+(rand()*(4-1))), "Fred", "Wang", "Fatimah", "Marcella")),
((floor(1+(rand()*(3-1))), "Kaser", "Fang", "Kumar")),
DATE_ADD('1965-01-01', INTERVAL rand()*200000 DAY));
set #i = #i+1;
end while;
END
Additionally, here is my volunteer table in my MYSQL script:
drop table if exists Volunteer;
create Table Volunteer(
member_num int not null auto_increment primary key,
firstname varchar(20) not null,
lastname varchar(20) not null,
dateofbirth date not null
);
I am trying to insert 500 lines into this table, however error 1305 is coming up.
Any help is heavily appreciated, I am quite unsure of where to go from this point.
This logic doesn't do anything:
(floor(1+(rand()*(4-1))), "Fred", "Wang", "Fatimah", "Marcella"))
Although not the most efficient, this should be fine for 500 rows:
insert into Volunteer(firstname, lastname, dateofbirth)
select f.firstname, l.lastname,
DATE_ADD('1965-01-01', INTERVAL rand()*200000 DAY)
from (select 'Fred' as firstname union all
select 'Wang' union all
select 'Fatimah' union all
select 'Marcella'
) f cross join
(select 'Kaser' as lastname union all
select 'Fang' union all
select 'Kumar'
) l
order by rand()
limit 1;
I think you are actually trying to write:
insert into Volunteer(firstname, lastname, dateofbirth)
select elt(floor(rand() * 4) + 1,
'Fred', 'Wang', 'Fatimah', 'Marcella'
) as firstname,
elt(floor(rand() * 3) + 1,
'Kaser', 'Fang', 'Kumar'
) as lastname,
DATE_ADD('1965-01-01', INTERVAL rand()*200000 DAY);

Optimize MySQL query - Data fetching form views table and Main table

I am fetching data from MySQL views table and Main table. I have created Indexes and Primary keys in Main table but I cannot create Indexes and primary keys on views table.
When I execute the below query it is taking around 10 seconds. I want to optimize the below query to less time.
SELECT DISTINCT
`Emp_No`, `Name`
FROM
`ResLookup`
WHERE
`IsActive` = 1
AND `Department` IN ('SDG' , 'HDD', 'ENG', 'PDN')
AND (`Emp_No` IN (SELECT DISTINCT
ProjList.PM_No
FROM
ProjList
WHERE
ProjList.PM_No != 1749 UNION SELECT DISTINCT
ProjList.PL_No
FROM
ProjList
WHERE
ProjList.PL_No != 1749)
OR Emp_No IN (SELECT
MEMBER_ID
FROM
s_group_details
WHERE
GROUP_ID = 'GRP109'
AND MEMBERSHIP_LEVEL = 30));
Only s_group_details table have Indexes and primary key. Remaining all tables are fetching from views table.
Using Explain Query I have the below output
I don't know your query requirements but still check below query helpful or not
SELECT DISTINCT
`Emp_No`, `Name`
FROM
`ResLookup` inner join (SELECT DISTINCT
ProjList.PM_No ,ProjList.PL_No
FROM
ProjList
WHERE
ProjList.PM_No != 1749
or
ProjList.PL_No != 1749) a
on ResLookup.Emp_No = a.PM_No
and ResLookup.Emp_No = a.PL_No
OR Emp_No IN (SELECT
MEMBER_ID
FROM
s_group_details
WHERE
GROUP_ID = 'GRP109'
AND MEMBERSHIP_LEVEL = 30)
WHERE
`IsActive` = 1
AND `Department` IN ('SDG' , 'HDD', 'ENG', 'PDN');
It may be better to turn things somewhat inside-out:
SELECT `Emp_No`,
( SELECT Name
FROM ResLookup
WHERE Emp_No = u.PM_No
) AS Name
FROM
( SELECT PM_No FROM ProjList WHERE PM_No != 1749 )
UNION DISTINCT
( SELECT PL_No FROM ProjList WHERE PL_No != 1749 )
UNION DISTINCT
( SELECT MEMBER_ID
FROM s_group_details AS d
WHERE d.GROUP_ID = 'GRP109'
AND d.MEMBERSHIP_LEVEL = 30
) AS u
JOIN `ResLookup` AS r ON u.PM_No = r.Emp_No
WHERE r.`IsActive` = 1
AND r.`Department` IN ('SDG' , 'HDD', 'ENG', 'PDN');
Indexes needed:
ResLookup: (Emp_No, IsActive, Department)
s_group_details: (GROUP_ID, MEMBERSHIP_LEVEL, MEMBER_ID)

Lost connection to MySQL server during query with UNION

i am getting error Lost connection to MySQL server during query with UNION query bellow.
select *
from `dealers`
where exists ((
select *
from `cars`
where `dealers`.`id` = `cars`.`dealer_id`
and exists (select *
from `dealers`
where `cars`.`dealer_id` = `dealers`.`id`)
and `dealer_id` = 27
order by `price` asc)
union (select *
from `cars`
where `dealers`.`id` = `cars`.`dealer_id`
and exists (select *
from `dealers`
where `cars`.`dealer_id` = `dealers`.`id`)
and `dealer_id` != 27
order by `price` desc))
order by `dealers`.`name` asc
Strange thing is that if i remove the last line entirely or if i sort by id instead of name it works fine.
Any ideas what could possibly be wrong with this?

MYSQL Stored Procedures INSERT Fails

I am using MYSQL version 5.5
I am trying to insert the following procedure:
CREATE PROCEDURE `myTestProceed3ure` ( IN _id INT )
IF( SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid ) >0
THEN
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
ELSE
(INSERT INTO `tbl_search_counter` (`user_id` ,`time_searched`) VALUES ('165', '7'));
END IF
But for whatever reason I get the following error message
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'INSERT INTO tbl_search_counter (user_id ,time_searched)
VALUE' at line 6
I really don't understand because when I do the following request I do not experience problem
CREATE PROCEDURE `myTestProceed2ure` ( IN `_id` INT )
INSERT INTO `tbl_search_counter` (`user_id`, `time_searched`) VALUES ('15', '7')
or the following
CREATE PROCEDURE `myTestProceed3ure` ( IN _id INT )
IF( SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid ) >0
THEN
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
ELSE
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
END IF
If someone has any idea what I am doing wrong, let me know...
Thanks
What is your stored procedure supposed to be doing? You are not referencing the parameter to the stored procedure and u_userid seems undefined.
If you just want to insert an id that doesn't currently exist, then here is one way:
CREATE PROCEDURE `myTestProceed3ure` (IN _id INT )
INSERT INTO tbl_search_counter(user_id, time_searched)
VALUES (_id, '7')
ON DUPLICATE KEY UPDATE user_id = values(user_id)
END IF;
Final answer: I should not have put () before the INSERT...
CREATE PROCEDURE `myTestProceed3ure` ( IN _id INT )
IF( SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid ) >0
THEN
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
ELSE
INSERT INTO `tbl_search_counter` (`user_id` ,`time_searched`) VALUES ('165', '7');
END IF