MySql assigning a select - mysql

I am trying to create a MySql function that will tell me how many orders(comanda) have been placed by a client(whose id i will provide as a parameter).
However, I am getting a syntax error that says counter(my decared variable) is not valid at this position, expecting an identifier.
I split the declaration and both assignments of the value just to be sure that this is not the reason for the error. The error triggers at the last assignment of the select.
Can anybody explain what I am doing wrong? Thank you!
create table client
(id int primary key auto_increment,
nume varchar(50),
prenume varchar(50),
oras varchar(50),
judet varchar(50),
strada varchar(50),
cod_postal varchar(50),
nr_tel varchar(50),
email varchar(50)
);
create table comanda
(id int primary key auto_increment,
data_plasare date,
metoda_livrare enum('ridicare personala','livrare la domiciliu','easy box'),
metoda_plata enum('numerar','card la ghiseu','online cu cardul','transfer bancar','PayPal','rate'),
id_client int
);
alter table comanda
add foreign key (id_client) references client(id);
delimiter //
create function clienti_multiple_comenzi(p_id_client int)
returns int
begin
declare counter int;
set counter = 0;
set counter = select count(id)
from comanda
where id_client = p_id_client;
return counter;
end;
// delimiter ;

Related

Mysql with function

CREATE TABLE `emp_info` (
id INT NOT NULL AUTO_INCREMENT,
user_id INT,
first_name VARCHAR(70),
last_name VARCHAR(70),
PRIMARY KEY (id),
KEY (user_id)
);
CREATE TABLE `message_info` (
id INT NOT NULL AUTO_INCREMENT,
user_id INT,
message MEDIUMTEXT,
PRIMARY KEY (id),
CONSTRAINT fk_message FOREIGN KEY(user_id) REFERENCES emp_info(user_id)
);
INSERT INTO emp_info(user_id,first_name,last_name) VALUES
(001,'Ashley','Smith'),
(002,'Valen','Hue'),
(003,'Mia','Cmd'),
(004,'Alex','Smith'),
(005,'Dy','Thomson'),
(006,'Ashley','Smith'),
(007,'Alex','Smith'),
(008,'Dy','Thomson');
INSERT INTO message_info(message) VALUES
('This is a message1 '),
('This is a message2'),
('This is not messge3'),
('This is a messagee4'),
('This is a messge5');
Hi, all I need mini help with the function.
Create a function with parameters of id user to count and return the total number of messages he/she has sent.
if we are talking about a specific id, the query is like:
SELECT count(*) FROM message_info where message_info.user_id = ´your_id´
To create a function is like:
DELIMETER $$
CREATE FUNCTION count_message(id INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE count_message INT;
select count(*) INTO count_message FROM message_info where message_info.user_id = ´your_id´;
return count_message;
END$$
DELIMITER ;
in you query insert you are missing the id user, so you need add it.

MySQL Trigger syntax for ordering based on user id

I would like to create a trigger on the FRIENDS table so that friend pairs are ordered and there aren't duplicate entries with the same friends. The following trigger is throwing a syntax error at BEGIN IF (:new.USER1_ID > :new.USER2_ID) ... -- how is this trigger supposed to be formatted?
CREATE TABLE USERS (
USER_ID INTEGER NOT NULL,
FIRST_NAME VARCHAR(100) NOT NULL,
LAST_NAME VARCHAR(100) NOT NULL,
YEAR_OF_BIRTH INTEGER,
MONTH_OF_BIRTH INTEGER,
DAY_OF_BIRTH INTEGER,
SSN INTEGER,
EMAIL VARCHAR(100),
PHONE INTEGER,
PWD VARCHAR(100),
ADDRESS VARCHAR(500),
PRIMARY KEY(USER_ID)
);
CREATE TABLE FRIENDS (
USER1_ID INTEGER,
USER2_ID INTEGER,
FOREIGN KEY (USER1_ID) REFERENCES USERS(USER_ID) ON DELETE CASCADE,
FOREIGN KEY (USER2_ID) REFERENCES USERS(USER_ID) ON DELETE CASCADE,
PRIMARY KEY(USER1_ID, USER2_ID),
CHECK(USER1_ID!=USER2_ID)
);
CREATE TRIGGER FRIENDS_TRIGGER
BEFORE INSERT ON FRIENDS FOR EACH ROW
BEGIN
DECLARE TEMP INTEGER
BEGIN
IF (:new.USER1_ID > :new.USER2_ID) THEN
SET TEMP = :new.USER1_ID
SET :new.USER1_ID = :new.USER2_ID
SET :new.USER2_ID = TEMP
END IF
END
/
There are several problems. You need to terminate statements with semicolons, and so you also need to change the delimiter so the entire create trigger is treated as a single statement. :new should also be NEW. And you have an extra BEGIN thrown in there.
DELIMITER //
CREATE TRIGGER FRIENDS_TRIGGER
BEFORE INSERT ON FRIENDS
FOR EACH ROW
BEGIN
DECLARE TEMP INTEGER;
IF (NEW.USER1_ID > NEW.USER2_ID) THEN
SET TEMP = NEW.USER1_ID;
SET NEW.USER1_ID = NEW.USER2_ID;
SET NEW.USER2_ID = TEMP;
END IF;
END; //
DELIMITER ;

my procedure with cursor in mysql

I have a problem and I don't know how to solve it.
I need to create a procedure with cursor to insert into Vizite: dataora (i need a function), Medici_Idm,Pacienti_Idm,Cabinete_Idm from temporary table.My procedure doesn't work, i also use for select tables: Medici, Pacineti,Cabinete to extract id.
Table 1:
CREATE TABLE IF NOT EXISTS Vizite (
Idv INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
DataOra DATETIME,
Medici_Idm INT NOT NULL,
Pacienti_Idp INT NOT NULL,
Cabinete_Idc INT NOT NULL,
FOREIGN KEY (Medici_Idm) REFERENCES Medici(Idm),
FOREIGN KEY (Pacienti_Idp) REFERENCES Pacienti(Idp),
FOREIGN KEY (Cabinete_Idc) REFERENCES Cabinete(Idc)
) Engine=INNODB;
Table 2:
CREATE TEMPORARY TABLE IF NOT EXISTS TempVizite (
Idt INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
DataVizita VARCHAR(250),
OraIntrare TIME,
NumePacient VARCHAR(250),
PrenumePacient VARCHAR(250),
NumeMedic VARCHAR(250),
PrenumeMedic VARCHAR(250),
Cabinet VARCHAR (250));
the procedure:
DELIMITER /
CREATE PROCEDURE VARIANTA3()
BEGIN
DECLARE M_I,P_I,C_I VARCHAR(100);
DECLARE I_V INT;
DECLARE D DATE;
DECLARE O TIME;
DECLARE CURS1 CURSOR FOR -- declaram cursorul
SELECT IDT,DATAVIZITA,ORAINTRARE,NUMEMEDIC,NUMEPACIENT,CABINET FROM TEMPVIZITE;
DECLARE EXIT HANDLER FOR 1329 BEGIN END; -- declaram handler specific erorii de terminarea cursorului
OPEN CURS1;
BUCLA: LOOP
FETCH CURS1 INTO M_I,P_I,C_I,I_V,D,O;
INSERT IGNORE INTO VIZITE VALUES(I_V,#DATAORA,M_I,P_I,C_I);
SELECT TIMESTAMP(DATAVIZITA,ORAINTRARE) INTO #DATAORA;
SELECT IDM FROM MEDICI WHERE NUME = NUMEMEDIC INTO M_I;
SELECT IDP FROM PACIENTI WHERE NUME = NUMEPACIENT INTO P_I;
SELECT IDC FROM CABINETE WHERE NUME = CABIENT INTO C_I;
END LOOP;
CLOSE CURS1;
END /
DELIMITER ;

Stored procedure to select column from table and insert to multiple table in mysql

I want to select values from basic2 and insert into basic3 and basic4 using stored procedure.
These are the table definitions:
create table basic2(
id int AUTO_INCREMENT,
name varchar(50),
address varchar(50),
PRIMARY KEY (id)
);
create table basic3(
id int AUTO_INCREMENT,
name varchar(50),
address varchar(50),
PRIMARY KEY (id)
);
create table basic4(
id int AUTO_INCREMENT,
name varchar(50),
address varchar(50),
PRIMARY KEY (id)
);
this is the new_person store procedure
drop procedure if exists new_person;
DELIMITER //
CREATE PROCEDURE new_person
select (id, name,address)
from basic2;
BEGIN
START TRANSACTION;
INSERT INTO basic3 (id,name,address)
VALUES(LAST_INSERT_ID(),bname,baddress);
INSERT INTO basic4 (id,name,address)
VALUES(LAST_INSERT_ID(),bname,baddress);
COMMIT;
END//
DELIMITER;
We can do it by two way one for using cursor and another is using SELECT with insert i thing for you SELECT is better
Like this
INSERT INTO basic3 (name,address)
SELECT name, address FROM basic2;

mySQL Stored Procedure - Cursor error

I'm trying to write a mySQL procedure with a cursor to calculate a fare. I pass in the stationid's then I figure out what zone they are in. The fare is a set value of $1 and an additional $0.20 for each zone traveled in.
The code I have so far runs but there is a problem with the cursor not fetching the values into the variables.
Any help would be greatly appreciated. Thanks
Tables:
DROP DATABASE IF EXISTS luasSystem;
CREATE DATABASE luasSystem;
USE luasSystem;
CREATE TABLE IF NOT EXISTS line
(
line_id INT NOT NULL AUTO_INCREMENT,
Line_colour CHAR(10) NOT NULL,
PRIMARY KEY (line_id)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS zone
(
zone_id INT NOT NULL AUTO_INCREMENT,
zone_name VARCHAR(20) NOT NULL,
line INT NOT NULL,
PRIMARY KEY (zone_id),
FOREIGN KEY (line) REFERENCES line(line_id) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS station
(
station_id INT NOT NULL AUTO_INCREMENT,
station_name CHAR(20) NOT NULL,
service CHAR(20),
line INT NOT NULL,
zone INT NOT NULL,
PRIMARY KEY (station_id),
FOREIGN KEY (line) REFERENCES line(line_id) ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (zone) REFERENCES zone(zone_id) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB;
Stored Procedure:
DROP PROCEDURE IF EXISTS calculateFare;
DELIMITER //
CREATE PROCEDURE calculateFare
(
IN stationid1 INT, IN stationid2 INT
)
BEGIN
DECLARE zoneNum1 INT;
DECLARE zoneNum2 INT;
DECLARE num INT;
DECLARE fare DOUBLE;
DECLARE tableEnd BOOLEAN;
DECLARE zoneCur CURSOR FOR
SELECT zone, zone FROM station
WHERE station_name = stationid1 AND station_name = stationid2;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET tableEnd = TRUE;
OPEN zoneCur;
the_loop: LOOP
FETCH zoneCur
INTO zoneNum1, zoneNum2;
IF tableEnd THEN
CLOSE zoneCur;
LEAVE the_loop;
END IF;
SET fare = 1;
SET num = 0;
IF zoneNum1 < zoneNum2 THEN
SET num = zoneNum2 - zoneNum1;
ELSEIF zoneNum1 > zoneNum2 THEN
SET num = zoneNum1 - zoneNum2;
END IF;
SET fare = (num * 0.20) + 1;
SELECT fare;
END LOOP the_loop;
END //
DELIMITER ;
CAll calculateFare(3,5);
Wouldn't this be easier of you used decimal values instead of integers? Suggest you look at : http://dev.mysql.com/doc/refman/5.0/en/fixed-point-types.html