Error in creating a stored procedure - mysql

I'm trying to retrieve values by joining two tables (the customer and enquiry table),then i'm trying to store the retrieved values into another table that would come in handy for reasons irrelevant here.And then i'm finally deleting the retrieved values from the enquiry table. When i'm trying to execute the stored procedure i'm getting the following error shown in the screenshot below.
how do i resolve this error?
Stored Procedure:-
CREATE PROCEDURE `backup_eq`(
IN `eq` VARCHAR(15), IN `mail` VARCHAR(30), IN `dates` DATE, IN `cmp` VARCHAR(10), IN `rea` VARCHAR(50))
NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER
BEGIN
SELECT eqno into #eno,Date1 into #d,cmpname into #c,subject into #s,cid into #cd
FROM `enquiry` NATURAL JOIN `customer`
WHERE eqno=eq and email=mail and cmpname=cmp and Date=dates;
INSERT INTO `enquiryBin`(`Eqno`, `Date1`, `Cmpname`, `Subject`, `CID`, `Reason`)
VALUES (#eno,#d,#c,#s,#cd,rea);
DELETE FROM `enquiry`
WHERE eqno=eq and cid=#cd and cmpname=cmp and Date1=dates;
END
The create table statements of the two tables are given below
CREATE TABLE `customer` (
`CID` int(15) NOT NULL,
`Address` varchar(100) NOT NULL,
`Name` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`phone` bigint(20) NOT NULL
)
ALTER TABLE `customer`
ADD PRIMARY KEY (`CID`);
CREATE TABLE `enquiry` (
`Eqno` varchar(15) NOT NULL,
`Date1` date NOT NULL,
`Cmpname` varchar(10) NOT NULL,
`Subject` varchar(100) NOT NULL,
`CID` int(15) NOT NULL
)
ALTER TABLE `enquiry`
ADD PRIMARY KEY (`Eqno`,`Cmpname`,`CID`,`Date1`)

SELECT eqno into #eno,Date1 into #d,cmpname into #c,subject into #s,cid into #cd
Should be
SELECT eqno, Date1, cmpname, subject, cid INTO #eno, #d, #c, #s, #cd
That is, name all columns in the select-list separately from the INTO clause.
Refer to syntax documentation: https://dev.mysql.com/doc/refman/5.7/en/select-into.html

There's no need for all those variables, just use an INSERT INTO ... SELECT query, and a JOIN in the DELETE query.
INSERT INTO enquiryBin (`Eqno`, `Date1`, `Cmpname`, `Subject`, `CID`, `Reason`)
SELECT eqno, Date1, cmpname, subject, cid, rea
FROM FROM `enquiry` NATURAL JOIN `customer`
WHERE eqno=eq and email=mail and cmpname=cmp and Date1 = dates;
DELETE e FROM enquiry AS e
NATURAL JOIN customer
WHERE eqno = eq AND email = mail AND cmpname = cmp AND Date1 = dates

Related

sql JOIN cause double rows

I have two tables that i need to join them
first table i created using this sql query
CREATE TABLE `users` (
`id` bigint PRIMARY KEY AUTO_INCREMENT,
`user_login` varchar(60),
`user_pass` varchar(255),
`first_name` varchar(30),
`last_name` varchar(30),
`user_status` int(2),
`user_phone_number` varchar(20),
`user_email` varchar(100),
`user_billing_info` text,
`user_temp_units` int(2),
`user_flow_units` int(2),
`user_notes` text
);
second table
CREATE TABLE `station_meta` (
`uid` VARCHAR(25) PRIMARY KEY,
`nickname` varchar(30),
`install_date` date,
`latatude` numeric(10,6),
`longitude` numeric(10,6),
`firmware_ver` varchar(10),
`weir_type` int(2),
`weir_width` numeric,
`dist_to_ground` numeric,
`dist_to_weir` numeric,
`service_fee` numeric,
`notes` text
);
i got double rows when i use this sql query
SELECT * FROM station_meta JOIN users
note: uid is something like 9C9Z454Z5CA in case it need to mention it
so there's not any column that is the same in the other table
UPDATE
Data sample
My results
I'm using it in php function in foreach, so i got double results
Appreciate any help
seems you miss a relation between the two tables ..
if you want avoid cartesian product and retrieve just a matching value between the two table you should add a relation as
table user_station_meta (
`id` bigint PRIMARY KEY AUTO_INCREMENT,
user_id bigint
station_meta_uid VARCHAR(25)
)
once you have inserted the matching values
uid, id
9c2748.. 1
BC8CD4.. 5
you can select single matching result as
select u.*. s.*
from user_station_meta us
JOIN station_meta s on s.uid = us.uid
JOIN users u on u.id = us.id

sql how to use select query to get invoke foreign key

i was wondering how do i use the select query to Retrieve all offers of a listing.
Any tips or help to improve my codes is appreciated to! Thank you and have a nice day!
CREATE DATABASE IF NOT EXISTS `assignment_db`;
USE `assignment_db`;
CREATE TABLE USER_LIST(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
userName VARCHAR(50) NOT NULL,
email varchar(100) NOT NULL,
registeredDate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
create table listing_list(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
itemName VARCHAR(50) NOT NULL,
itemDescription VARCHAR(254) NOT NULL,
price DECIMAL(4,2) NOT NULL,
fk_poster_id int references USER_LIST(id),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
create table offer_list(
id int(6) Unsigned auto_increment Primary key,
offer int,
fk_listing_id int references listing_list(id),
fk_offeror_id int references user_list(id),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
insert into user_list (userName, email) values ('John','johnnyboi#123.com');
insert into user_list (userName, email) values ('Tom','Tommyboi#123.com');
insert into listing_list (itemName,itemDescription, price) values ( 'Pen', 'A long delicate pen.',' 1.50 ');
insert into listing_list (itemName,itemDescription, price) values ( 'Pencil', 'A long delicate pencil.',' 0.50 ');
insert into offer_list (offer,fk_listing_id,fk_offeror_id) values ('200','2','3');
insert into offer_list (offer,fk_listing_id,fk_offeror_id) values ('200','1','1');
All offer listing for existing user :
select a.*,b.*,c.* from offer_list a
INNER JOIN (listing_list as b)
on a.fk_listing_id=b.Id
INNER JOIN (user_list as c)
on a.fk_offeror_id=c.Id
just all offer listing :
select a.*,b.* from listing_list b
INNER JOIN (offer_list as a)
on a.fk_listing_id=b.Id
just all offer listing 2nd way
select a.*,b.* from offer_list a
INNER JOIN (listing_list as b)
on a.fk_listing_id=b.Id
Check it here , i tested them in this FIDDLE

MYSQL trigger insertion and updation errors : MySQL error 1241: Operand should contain 1 column(s)

I have 4 tables which look like....
CREATE TABLE `Faculty` (
`FID` varchar(10) NOT NULL,
`Name` varchar(30) NOT NULL,
`DOB` date NOT NULL,
`Sem` varchar(1) NOT NULL,
`Section` varchar(1) NOT NULL,
`Subject Code` varchar(6) NOT NULL,
`Dep` varchar(3) NOT NULL,
`Hours Taken` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Student` (
`USN` varchar(10) NOT NULL,
`DOB` date NOT NULL,
`Dep` varchar(3) NOT NULL,
`SEM` int(1) NOT NULL,
`Class` varchar(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Student Attendance` (
`USN` varchar(10) NOT NULL,
`Subject Code` varchar(6) NOT NULL,
`Attendance` int(11) DEFAULT '0',
`Absent Days` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Subjects` (
`Subject` varchar(40) NOT NULL,
`Subject Code` varchar(6) NOT NULL,
`Dep` varchar(3) NOT NULL,
`Sem` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Now I needed to make sure that the number of hours that a student (Student Attendance.Attendance) has attended aren't greater than the number of hours the faculty member has taken (Faculty.Hours Taken) for that particular subject (Subject Code). So I wrote a trigger to check to check on insertion and updation if the above condition is satisfied.
If the number of hours(Student Attendance.Attendance) are greater then I have set the Student Attendance.Attendance to some String. I'm hoping that this will work as an assertion and give me an error.
CREATE TRIGGER `HoursCheckonInsert` BEFORE INSERT ON `Student
Attendance`
FOR EACH ROW SET NEW.Attendance= IF(
( Select k.USN, s.`Subject Code`,f.`Hours Taken`,s.Attendance
From Faculty f, `Student Attendance` s, Student k
where s.`Subject Code` = f.`Subject Code` AND k.SEM = f.Sem AND k.Class=f.Section AND s.USN=k.USN AND
NEW.Attendance < f.`Hours Taken` OR NEW.Attendance = f.`Hours Taken`
),
NEW.Attendance,
'abcdef'
)
CREATE TRIGGER `HoursCheckonUpdate` BEFORE UPDATE ON `Student Attendance
FOR EACH ROW SET NEW.Attendance= IF(
(Select k.USN, s.`Subject Code`,f.`Hours Taken`,s.Attendance
From Faculty f, `Student Attendance` s, Student k
where NEW.`Subject Code`=f.`Subject Code` AND NEW.USN=k.USN AND
NEW.USN=s.USN AND k.SEM=f.Sem AND k.Class=f.Section AND k.DEP=f.DEP
AND (NEW.Attendance < f.`Hours Taken` OR NEW.Attendance = f.`Hours Taken`)
),
NEW.Attendance,
'abcdef'
)
When I try to insert a value into the Student Attendance, I get this error:
INSERT INTO `Student Attendance` (`USN`, `Subject Code`, `Attendance`, `Absent Days`) VALUES ('1KS15BT001', '15BT44', '0', '0');
Error:
#1241 - Operand should contain 1 column(s)
I'm new to triggers in MySQL.
MySQL's IF() function expects a scalar expression as its first argument. A scalar expression means it must be one column, and at most one row.
The subquery you use has four columns, and some unknown number of rows.
From your comments, it sounds like you're just interested in whether there are zero or more than zero rows matching the condition. You're right, it doesn't matter what columns you select—if you're using an EXISTS predicate to test the subquery instead of returning the result of the subquery in a scalar expression.
You should use the EXISTS predicate for what you want to do. See https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
CREATE TRIGGER `HoursCheckonInsert`
BEFORE INSERT ON `Student Attendance`
FOR EACH ROW
SET NEW.Attendance = IF(
EXISTS(
SELECT *
FROM Faculty AS f
JOIN `Student Attendance` AS s ON s.`Subject Code` = f.`Subject Code`
JOIN Student AS k ON k.SEM = f.Sem AND k.Class=f.Section AND k.uSN = s.USN
WHERE NEW.Attendance <= f.`Hours Taken`),
NEW.Attendance,
'abcdef');
Then it really doesn't matter what columns you put in the select-list. I show using SELECT * above. It doesn't matter, because EXISTS is only checking for the presence of any matching rows, and it won't have a result set at all—only the boolean for whether there are zero or more than zero rows.
You should also use JOIN syntax instead of the obsolete comma-style syntax for joins. I show that in the example above.

MySQL: Use substring to derive data from two different columns

I have the following table:
CREATE TABLE `vendor_contacts` (
`vendor_id` int(11) NOT NULL,
`last_name` varchar(50) NOT NULL,
`first_name` varchar(50) NOT NULL,
`name_initials` varchar(45) NOT NULL,
PRIMARY KEY (`vendor_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
With the following insert statement:
INSERT INTO `vendor_contacts`
VALUES (5,'Davison','Michelle',''),
(12,'Mayteh','Kendall',''),
(17,'Onandonga','Bruce',''),
(44,'Antavius','Anthony',''),
(76,'Bradlee','Danny',''),
(94,'Suscipe','Reynaldo',''),
(101,'O\'Sullivan','Geraldine',''),
(123,'Bucket','Charles','');
I would like to run a query that extracts the first letter from the first name and last name columns.
SELECT vendor_id, last_name, first_name, substring(first_name, 1, 1) AS initials
FROM vendor_contacts;
The following guide http://www.w3resource.com/mysql/string-functions/mysql-substring-function.php, only shows how to work with one column.
You pull them separately and combine them using concat():
SELECT vendor_id, last_name, first_name,
CONCAT(LEFT(first_name, 1), LEFT(last_name, 1)) as initials
FROM vendor_contacts;

JOIN LEFT with multiple conditions

I'm having following tables structure
CREATE TABLE IF NOT EXISTS `review_author` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`email` varchar(255) NOT NULL,
`client_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_37D99F0819EB6921` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2110 ;
AND
CREATE TABLE IF NOT EXISTS `brokers_comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hb_broker_id` int(11) NOT NULL,
`client_id` int(11) DEFAULT NULL,
`user_name` varchar(100) NOT NULL,
`user_email` varchar(100) NOT NULL,
`state` int(11) NOT NULL DEFAULT '0',
`text` varchar(3000) NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_5365DFFB9FE55EF7` (`hb_broker_id`),
KEY `IDX_5365DFFB19EB6921` (`client_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1583 ;
Before extracting value i did following query:
INSERT INTO review_author (
name,
email,
client_id
)
SELECT
brokers_comments.user_name,
brokers_comments.user_email,
brokers_comments.client_id
FROM brokers_comments
LEFT JOIN review_author
ON brokers_comments.user_name=review_author.name AND
brokers_comments.user_email=review_author.email AND
brokers_comments.client_id=review_author.client_id
WHERE review_author.id IS NULL
Not in review_author should be all author from table brokers_comments and now i'm trying to get authors id using following query:
SELECT
review_author.id
FROM brokers_comments
LEFT JOIN review_author
ON brokers_comments.user_name=review_author.name AND
brokers_comments.user_email=review_author.email AND
brokers_comments.client_id=review_author.client_id
WHERE review_author.id IS NOT NULL
but i'm getting about 110 results from total 1531 records from table brokers_comments.
UPDATE
I couldn't manage to insert data in http://sqlfiddle.com/ so following link are dump for two tables review_author and brokers_comments.
Again my issue is to transfer distinct columns(user_name, user_email, client_id) from table brokers_comments to table review_author and then select review_author.id based on relation name/email/client_id from both tables.
http://wrttn.in/7ca325
http://wrttn.in/3a7885
Insert new author was wrong and made duplication. Below is new correct form.
INSERT INTO review_author (
name,
email,
client_id
)
SELECT user_name, user_email, client_id
FROM brokers_comments AS broker
WHERE NOT EXISTS
(
SELECT 1
FROM review_author AS author
WHERE author.email = broker.user_email
)
GROUP BY broker.user_email
P.S. I somebody will make a working online mysql database please put in comments so i could put it there.
Resolved
Only now i realised that user_email must be unique. Based on this i made following select statement:
SELECT
author.id
FROM brokers_comments AS broker
LEFT JOIN review_author AS author
ON broker.user_email = author.email
It seems you use excess fields in JOIN clause since client_id is a key, you need to join tables only on this field. Possible cause of that you getting not same number of records is different name/email for same client_id in those two tables. So, your two queries should be like this:
INSERT INTO review_author (
name,
email,
client_id
)
SELECT
brokers_comments.user_name,
brokers_comments.user_email,
brokers_comments.client_id
FROM brokers_comments
LEFT JOIN review_author
ON brokers_comments.client_id=review_author.client_id
WHERE review_author.id IS NULL
and
SELECT
review_author.id
FROM brokers_comments
LEFT JOIN review_author
ON brokers_comments.client_id=review_author.client_id
WHERE review_author.id IS NOT NULL