Getting undsired output on mysql data queries mysql - mysql

The following is my SQL data structure I'm trying out two queries as follows:
Select
Wrk_ID, Wrk_LastName, Skill_Desc
from
Worker, Skill
where
(UPPER(skill_desc) = UPPER('Map Designer') or
UPPER(skill_Desc) = UPPER('Game Designer'));
I'm getting twice the output that I need
select
wrk_ID, concat('Wrk_FirstName','Wrk_LastName') As FullName,
COUNT(Assign_ID) as Max_Assignment
from
assignment worker
group by
Wrk_ID, concat('Wrk_FirstName','Wrk_LastName');
In this query I'm getting the wrk_firstname and last name instead of the actual data of first and last name.
--
-- Dumping data for table `region`
--
INSERT INTO `region` (`Reg_ID`, `Reg_desc`) VALUES
('R0010', 'California'),
('R0011', 'Nevada'),
('R0012', 'Arizona'),
('R0013', 'Michigan'),
('R0014', 'New York'),
('R0015', 'Texas'),
('R0016', 'Washington'),
('R0017', 'Oregon'),
('R0018', 'Ohio'),
('R0019', 'Boston');
-- --------------------------------------------------------
--
-- Table structure for table `client`
--
INSERT INTO `client` (`Cli_ID`, `Cli_FirstName`, `Cli_LastName`, `Cli_Initial`, `Cli_StreetAddress`, `Cli_Sex`, `Cli_Phone`,`Reg_ID`) VALUES
('C0010', 'Krishneel', 'Sharma', 'V', '99 Rock Road', 'M', '844-2573', 'R0010'),
('C0011', 'Ravneel', 'Sharma', 'V', 'Cecil Street', 'M', '894-1238', 'R0011'),
('C0012', 'Brad', 'Pitt', 'W', '615 Devil Road', 'M', '894-2285', 'R0012'),
('C0013', 'Tom', 'Cruz', 'F', '615 Gorge Town', 'M', '894-2180', 'R0013'),
('C0014', 'Paul', 'Walker', 'F', '615 Gran Street', 'M', '222-1672', 'R0014'),
('C0015', 'Jeniffer','Lopez', 'B', '713 Tall Road', 'F', '442-3381','R0015'),
('C0016', 'Brown', 'James', 'G', '615 South Place', 'F', '297-1228', 'R0016'),
('C0017', 'Lady', 'Gaga', 'G', '615 Hill Road', 'F', '290-2556', 'R0017'),
('C0018', 'Cristiano', 'Ronaldo', 'G', '713 Magic Road', 'M', '382-7185', 'R0018'),
('C0019', 'Lioneil', 'Messi', 'K', '615 Well Street', 'M', '297-3809', 'R0019');
-- --------------------------------------------------------
--
-- Table structure for table `worker`
--
INSERT INTO `worker` (`Wrk_ID`, `Wrk_FirstName`, `Wrk_LastName`, `Wrk_Initial`, `Wrk_HireDate`, `Wrk_BirthDate`, `Wrk_Phone`, `Wrk_IRD_Num`, `Reg_ID`) VALUES
('W0010', 'Rhonda', 'Lewis', 'G', '2000-03-21', '1985-02-19', '855-2573', 'X993473419', 'R0010'),
('W0011', 'Rhett', 'VanDam', 'V', '2002-03-19', '1985-01-11', '994-1238', 'X993473500', 'R0011'),
('W0012', 'Anne', 'Jones', 'M', '2000-04-11', '1965-08-19', '894-9985', 'X9934735001', 'R0012'),
('W0013', 'John', 'Lange', 'P', '2000-04-12', '1975-05-17', '894-5680', 'X993473502', 'R0013'),
('W0014', 'Robert', 'Williams', 'D', '2004-05-19', '1978-03-02', '244-1672', 'X993473503', 'R0014'),
('W0015', 'Jeanine','Smith', 'K', '2005-06-19', '1988-07-04', '434-3381', 'X993473506', 'R0015'),
('W0016', 'Brown', 'James', 'G', '2006-03-19', '1982-06-28', '297-1232', 'X993473507', 'R0016'),
('W0017', 'Jorge', 'Diante', 'K', '2007-01-19', '1984-11-19', '222-2556', 'X993473508', 'R0017'),
('W0018', 'Paul', 'Wiesenbach', 'R', '2007-01-19', '1976-12-12', '389-7185', 'X993473509', 'R0018'),
('W0019', 'Leighla', 'Genkazi', 'W', '2007-01-19', '1989-04-30', '292-3809', 'X993473600', 'R0019');
-- --------------------------------------------------------
--
-- Dumping data for table `project`
--
INSERT INTO `project`(`Proj_ID`,`Proj_Desc`,`Proj_Est_Start`,`Proj_Est_Finish`,`Proj_Cost`,`Proj_Act_Start`,`Proj_Act_Finish`,`Proj_Act_Cost`,`Cli_ID`) VALUES
('P0010','Map Design','2015-04-01','2015-04-30',10000.00,'2015-04-05','2015-05-01',12000.50,'C0010'),
('P0011','Level Design','2015-03-02','2015-03-15',5000.00,'2015-03-02','2015-03-15',7500.00,'C0011'),
('P0012','Controlls Design','2015-04-20','2015-05-01',20000.00,'2015-04-21','2015-04-30',20500.00,'C0012'),
('P0013','Character Design','2015-04-15','2015-05-11',30000.00,'2015-04-15','2015-05-12',50000.00,'C0010'),
('P0014','Sound Design','2015-01-01','2015-04-30',5000.00,'2015-01-10','2015-04-30',97560.00,'C0013'),
('P0015','Game Play design','2015-03-20','2015-03-30',57000.00,'2015-03-20','2015-03-25',6500.00,'C0014'),
('P0016','Server Design','2015-04-01','2015-04-30',89000.00,'2015-04-01','2015-04-30',8900.00,'C0015'),
('P0017','Online Gamplay Design','2015-05-01','2015-05-15',10000.00,'2015-05-02','2015-05-16',10000.00,'C0016'),
('P0018','Software Design','2015-04-01','2015-04-15',15000.00,'2015-04-01','2015-04-15',14500.00,'C0019'),
('P0019','Game programming','2015-01-14','2015-05-01',125000.00,'2015-01-14','2015-05-01',125000.00,'C0019');
-- --------------------------------------------------------
--
--Dumping data for table `assignment`
--
INSERT INTO `assignment`(`Assign_ID`,`Assign_Desc`,`Assign_Est_Start`,`Assign_Est_Finish`,`Assign_Act_Start`,`Assign_Act_Finish`,`Wrk_ID`,`Proj_ID`) VALUES
('A0010','Design Game maps','2015-03-15','2015-04-20','2015-03-15','2015-04-20','W0010','P0010'),
('A0011','Design All levels of Game','2015-03-02','2015-03-15','2015-03-02','2015-03-15','W0012','P0011'),
('A0012','Design Controls For all Platforms','2015-05-01','2015-05-20','2015-05-02','2015-05-21','W0013','P0012'),
('A0013','Design All Character of Game','2015-04-05','2015-04-30','2015-04-05','2015-05-01','W0014','P0013'),
('A0014','Game music Soundtracks','2015-01-03','2015-04-25','2015-02-05','2015-04-30','W0015','P0014'),
('A0015','Virtual walk through of game','2015-03-15','2015-03-30','2015-03-15','2015-03-30','W0016','P0015'),
('A0016','MultiPlayer Game Design','2015-04-02','2015-04-28','2015-04-04','2015-04-30','W0017','P0016'),
('A0017','Online Hosting Design','2015-05-01','2015-05-16','2015-05-01','2015-05-30','W0018','P0017'),
('A0018','Game Interesction Design','2015-04-01','2015-04-15','2015-04-01','2015-04-15','W0011','P0018'),
('A0019','Game codes','2015-01-14','2015-05-05','2015-01-14','2015-05-05','W0019','P0019');
-- --------------------------------------------------------
--
----Dumping data for table `task`
--
INSERT INTO `task`(`Task_ID`,`Task_Desc`,`Task_Act_Start`,`Task_Act_Finish`,`Assign_ID`) VALUES
('T0010','Impliment Map Design','2015-03-15','2015-04-20','A0010'),
('T0011','Impliment game Levels','2015-03-02','2015-03-15','A0011'),
('T0012','Impliment Game Controls','2015-05-02','2015-05-21','A0012'),
('T0013','Impliment Game Characters','2015-02-05','2015-04-30','A0013'),
('T0014','Impliment Sound Design','2015-03-15','2015-03-30','A0014'),
('T0015','Impliment Gamplay designs','2015-04-04','2015-04-30','A0015'),
('T0016','Office Management Systems','2015-05-01','2015-05-30','A0016'),
('T0017','Impliment Multiplayer Gamplay','2015-04-01','2015-04-15','A0017'),
('T0018','Impliment Online Servers','2015-01-14','2015-05-05','A0018'),
('T0019','Impliment all Game Codes','2015-01-14','2015-05-05','A0019');
-- --------------------------------------------------------
--
----Dumping data for table `invoice`
--
INSERT INTO `invoice`(`Inv_ID`,`Inv_Amount`,`Inv_Due_Date`,`Proj_ID`) VALUES
('I0010',12000.50,'2015-04-30','P0010'),
('I0011',7500.00,'2015-03-31','P0011'),
('I0012',20500.00,'2015-04-30','P0012'),
('I0013',50000.00,'2015-05-30','P0013'),
('I0014',97560.00,'2015-05-30','P0014'),
('I0015',6500.00,'2015-04-30','P0015'),
('I0016',8900.00,'2015-05-30','P0016'),
('I0017',10000.00,'2015-05-30','P0017'),
('I0018',14500.00,'2015-04-30','P0018'),
('I0019',125000.00,'2015-05-15','P0019');
-- --------------------------------------------------------
--
----Dumping data for table `skill`
--
INSERT INTO `skill` (`Skill_ID`,`Skill_Desc`,`Skill_PayRate`) VALUES
('S0010','Map Designer',55.00),
('S0011','Level Designer',40.00),
('S0012','Game Designer',65.00),
('S0013','Graphics Designer',55.00),
('S0014','Music Producer',50.00),
('S0015','Project manager',98.50),
('S0016','Software Enginneer',50.00),
('S0017','IT Technician',40.50),
('S0018','Server Adminstrator',62.00),
('S0019','Network Enginneer',125.00);
-- --------------------------------------------------------
--
----Dumping data for table `employee_skill`
--
INSERT INTO `employee_skill`(`Skill_ID`,`Wrk_ID`) VALUES
('S0010','W0010'),
('S0011','W0011'),
('S0012','W0012'),
('S0013','W0013'),
('S0014','W0014'),
('S0015','W0015'),
('S0016','W0016'),
('S0017','W0017'),
('S0018','W0018'),
('S0019','W0019');

1) your first query need join conditions such as
where worker.wrkid= skill.wrkid and ....
2) your second query has an error, you need a comma between assignment and worker. Also join conditions are required.

Related

Column count doesn't match value count at row 1 - MySQL

So I created a table name locations:
create table locations (
location_id int primary key,
street_address varchar(50),
postal_code varchar(30),
city char(20),
state_province char(30),
country_id char(5)
);
and I was supposed to input these data in the table locations:
insert into locations values(1000, '1297 Via Cola di Rie', '989', 'Roma', null, 'IT');
insert into locations values(1100, '93091 Calle della Te', '10934', 'Venice', null, 'IT');
insert into locations values(1200, '2017 Shinjuku-ku', '1689', 'Tokyo', 'Tokyo Prefecture', 'JP');
insert into locations values(1300, '9450 Kamiya-cho', '6823', 'Hiroshima', null, 'JP');
insert into locations values(1400, '2014 Jabberwocky Rd', '26192', 'Southlake Texas', 'Texas', 'US');
insert into locations values(1500, '2011 Interiors Blvd', '99236', 'South San', 'California', 'US');
insert into locations values(1600, '2007 Zagora St', '50090', 'South Brun', 'New Jersey', 'US');
insert into locations values(1700, '2004 Charade Rd', '98199', 'Seattle', 'Washington', 'US');
insert into locations values(1800, '147 Spadina Ave', 'MSV 2L7', 'Toronto', 'Ontario', 'CA');
insert into locations values(1900, '6092 Boxwood St', 'YSW 9T2', 'Whitehorse', 'Yukon', 'CA');
insert into locations values(2000, '40-5-12 Laogianggen', '190518', 'Beijing', null, 'CN');
insert into locations values(2100, '1298 Vilaparle (E)', '490231', 'Bombay', 'Maharashtra', 'IN');
insert into locations values(2200, '12-98 Victoria Street', '2901', 'Sydney', 'New South Wale', 'AU');
insert into locations values(2300, '198 Clementi Norrth', '540198', 'Singapore', null, 'SG');
insert into locations values(2400, '8204 Arthur St', null, 'London', null, 'UK');
insert into locations values(2500, 'Magdalen Centre, The OX9 9ZB', 'Oxford', 'Oxford', 'UK');
insert into locations values(2600, '9802 Chester Road', '9629850293', 'Stretford', 'Manchester', 'UK');
insert into locations values(2700, 'Schwanthalerstr. 703', '80925', 'Munich', 'Bavaria', 'DE');
insert into locations values(2800, 'Rua Frei Caneca 1360', '01307-002', 'Sao Paulo', 'Sao Paulo', 'BR');
insert into locations values(2900, '20 Rue des Corps-Sai', '1730', 'Geneva', 'Geneve', 'CH');
insert into locations values(3000, 'Murtenstrasse 921', '3095', 'Bern', 'BE', 'CH');
insert into locations values(3100, 'Pieter Breughlstraa', '3029SK', 'Utrecht', 'Utrecht', 'NL');
insert into locations values(3200, 'Mariano Escobedo 999', '11932', 'Mexico Cit', 'Distrito Feder','MX');
Everything was going fine till 1900, but when I tried to execute insert command from the 2000 it gives me this error message "Column count doesn't match value count at row 1".
How can I fix this?
Sorry if my question is dumb or obvious, I just started learning databases and mysql
When inserting data into a MySQL table, you will have to specify which data goes into which column. You do this by specifying the column names before the VALUES part like:
INSERT INTO table (col1, col2) VALUES ('value1','value2');

Using mysql: column count doesn't match value count at row 1

I'm new to mysql, and can not figure out why this error keeps coming up. It's a simple table and I want id to be 1, 2, 3, 4 etc. alongside two other columns. Why does it keep reading, column count doesn't match value count at row 1?
CREATE DATABASE thedatabase;
USE thedatabase;
CREATE TABLE cars (
id INTEGER AUTO_INCREMENT,
model INTEGER NOT NULL,
mileage INTEGER NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO thedatabase.cars (
model,
mileage
) VALUES (
(45, 34598),
(22, 23847),
(10, 3847),
(487, 93229),
(237, 238975),
(23, 23987),
(34, 3498),
(57, 34984),
(56, 34983),
(20, 9845);
You have got an extra opening bracked in INSERT statement, after VALUES below should work fine:
INSERT INTO thedatabase.cars ( model,
mileage ) VALUES (45, 34598), (22, 23847), (10, 3847), (487, 93229), (237, 238975), (23, 23987), (34, 3498), (57, 34984), (56, 34983), (20, 9845);

Conversion failed when converting date and / or time when creating a table

So I'am making a table with a char, varchar and date. I got an error message saying "Conversion failed when converting date and / or time". If anyone can help me fix, this you got sincere thank you. :D
this is my code for creating and inserting data on my table:
Create table Employee
(EMP_NUM char(3),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL char(1),
EMP_HIREDATE date,
JOB_CODE char (3),
EMP_YEARS char(2))
Insert into Employee (EMP_NUM, EMP_LNAME, EMP_FNAME, EMP_INITIAL,
EMP_HIREDATE, JOB_CODE)
Values (101, 'News', 'John', 'G','08-Nov-00', 502),
(102, 'Senior', 'David', 'H','12-Jul-89', 501),
(103, 'Arbough', 'June', 'E','01-Dec-96', 500),
(104, 'Ramoras', 'Anne', 'K','15-Nov-87', 501),
(105, 'Johnson', 'Alice', 'k','01-Feb-93', 502),
(106, 'Smithfield', 'William', null, '22-Jun-04', 500),
(107, 'Alonzo', 'Maria', 'D','10-Oct-93', 500),
(108, 'Washington', 'Ralph', 'B', '22-Aug-91',501),
(109, 'Smith', 'Larry', 'W', '18-Jul-97', 501),
(110, 'Olenko', 'Gerald', 'A', '11-Dec-95', 505),
(111, 'Wabash', 'Geoff', 'B', '04-Apr-91', 506),
(112, 'Smithson', 'Darlene', 'M', '23-Oct-94', 507),
(113, 'Joenbrood', 'Delbert', 'K', '15-ov-96', 508),
(114, 'Jones', 'Annelise', null, '20-Aug-93', 508),
(115, 'Bawangi', 'Travis', 'B','25-Jan-92', 501),
(116, 'Pratt', 'Gerald', 'L','05-Mar-97', 510),
(117, 'Williamson', 'Angie', 'M', '19-Jun-96', 509),
(118, 'Frommer', 'james', 'J','04-Jan-05', 510);
and this is the complete message result :
Msg 241, Level 16, State 1, Line 11
Conversion failed when converting date and/or time from character string.
Use CONVERSION for EMP_HIREDATE column for date :
For ex :
SELECT CAST('18-Jul-97' AS DATE)
In your query :
Insert into Employee (EMP_NUM, EMP_LNAME, EMP_FNAME, EMP_INITIAL,
EMP_HIREDATE, JOB_CODE)
SELECT 101, 'News', 'John', 'G',CAST('08-Nov-00' AS DATE), 502

restore a deleted data in my table to its original status

is there an sql code that will restore the data from my table, the table should contain the data that existed before I made the changes.
Here is my original data:
Insert into EMP_1 (EMP_NUM, EMP_LNAME,EMP_FNAME,EMP_INITIAL, EMP_HIREDATE, JOB_CODE)
values (103, 'Arbough', 'June', 'E', '01-Dec-96', 503),
(104, 'Ramoras', 'Anne', 'K','15-Nov-87', 501),
(105, 'Johnson', 'Alice', 'k','01-Feb-93', 502),
(106, 'Smithfield', 'William', null, '22-Jun-04', 500),
(107, 'Alonzo', 'Maria', 'D','10-Oct-93', 500),
(108, 'Washington', 'Ralph', 'B', '22-Aug-91', 501),
(109, 'Smith', 'Larry', 'W', '18-Jul-97', 501);
and heres the result after i deleted William Smithfield:
this is my code for deleting it:
Delete from EMP_1
where EMP_FNAME = 'William'
and EMP_LNAME = 'Smithfield'
and EMP_HIREDATE = '2004-06-22'
and JOB_CODE = 500
If you want to recover the data, no it is not possible with no history table.
Is there is a reason to do hard delete, I would suggest you to use soft delete, as in to have IsDeleted column in the table, and make it true or false, if you are not having and history tables.
if you want to reenter the data which you had, and you have first column as Identity column, then Set Identity off and on, and insert the data.

MySQL query comparing values to previous rows' values

I've been searching but have been unable to find a solution to this--I know it's do-able but I just don't have the ninja SQL skills I need (yet)....
I'm looking for a solution to this issue: I have a 2 tables related to stock market data. The first is a simple list of stock symbols with an ID and stock ticker symbol (ID,SYMBOL). The second table contains historical price data for each of the stocks. (ID, DATE, OPEN, HIGH, LOW, CLOSE, VOLUME).
I'm trying to figure out how to query for stocks that have the most recent CLOSE price that is greater than their CLOSE price 5 trading-days ago. I can't just do date math because the stocks don't trade every day (no trading on weekends & holidays, as well as some stocks may not trade on a normal trading day). Thus, I just need to compare the CLOSE price from most recent row and the 5th row proceeding it for each symbol.
I have sample tables and data here:
http://sqlfiddle.com/#!2/5fe76/2
CREATE TABLE `STOCKS` (
`ID` int,
`SYMBOL` varchar(10)
);
INSERT INTO `STOCKS` (`ID`,`SYMBOL`)
VALUES
(1, 'AA'),
(2, 'ADT'),
(3, 'AEO'),
(4, 'AFA');
CREATE TABLE `PRICES` (
`ID` int,
`DATE` date,
`OPEN` decimal(6,2),
`HIGH` decimal(6,2),
`LOW` decimal(6,2),
`CLOSE` decimal(6,2),
`VOLUME` bigint
);
INSERT INTO `PRICES` (`ID`,`DATE`,`OPEN`,`HIGH`,`LOW`,`CLOSE`,`VOLUME`) VALUES
(1, '2014-11-06', 16.37, 16.42, 16.15, 16.37, 14200400),
(1, '2014-11-05', 16.68, 16.69, 16.17, 16.26, 18198200),
(1, '2014-11-04', 16.85, 16.87, 16.43, 16.56, 13182800),
(1, '2014-11-03', 16.78, 17.03, 16.65, 16.93, 15938500),
(1, '2014-10-31', 16.43, 16.76, 16.24, 16.76, 18618300),
(1, '2014-10-30', 16.17, 16.36, 15.83, 16.22, 17854400),
(1, '2014-10-29', 16.58, 16.70, 16.05, 16.27, 31173000),
(1, '2014-10-28', 16.5, 16.65, 16.41, 16.60, 12305900),
(1, '2014-10-27', 16.56, 16.57, 16.31, 16.38, 15452900),
(1, '2014-10-24', 16.33, 16.57, 16.22, 16.55, 12840200),
(2, '2014-11-06', 35.9, 36.12, 35.75, 36.07, 1018100),
(2, '2014-11-05', 35.68, 35.99, 35.37, 35.96, 1101500),
(2, '2014-11-04', 35.13, 35.69, 35.02, 35.49, 819100),
(2, '2014-11-03', 35.81, 35.99, 35.27, 35.32, 1304500),
(2, '2014-10-31', 35.79, 35.86, 35.46, 35.84, 1319400),
(2, '2014-10-30', 34.7, 35.34, 34.66, 35.19, 1201800),
(2, '2014-10-29', 35.06, 35.56, 34.5, 34.92, 1359000),
(2, '2014-10-28', 34.32, 35.17, 34.15, 35.07, 1301800),
(2, '2014-10-27', 34.2, 34.2, 33.66, 34.1, 662600),
(2, '2014-10-24', 34.02, 34.54, 33.95, 34.5, 750600),
(3, '2014-11-06', 13.27, 13.92, 13.25, 13.82, 6518000),
(3, '2014-11-05', 12.95, 13.27, 12.74, 13.22, 8716700),
(3, '2014-11-04', 12.85, 12.94, 12.65, 12.89, 4541200),
(3, '2014-11-03', 12.91, 13.12, 12.73, 12.89, 4299100),
(3, '2014-10-31', 13.2, 13.23, 12.83, 12.87, 7274700),
(3, '2014-10-30', 12.83, 12.91, 12.68, 12.86, 4444300),
(3, '2014-10-29', 13.02, 13.20, 12.79, 12.91, 2974900),
(3, '2014-10-28', 12.87, 13.10, 12.52, 13.04, 7365600),
(3, '2014-10-27', 12.84, 13.00, 12.67, 12.92, 6647900),
(3, '2014-10-24', 13.26, 13.29, 12.60, 12.92, 12803300),
(4, '2014-11-06', 24.59, 24.59, 24.49, 24.55, 20400),
(4, '2014-11-05', 24.81, 24.9, 24.81, 24.88, 11800),
(4, '2014-11-04', 24.87, 24.88, 24.76, 24.88, 10600),
(4, '2014-11-03', 24.85, 24.88, 24.76, 24.81, 18100),
(4, '2014-10-31', 24.82, 24.85, 24.77, 24.78, 8100),
(4, '2014-10-30', 24.83, 24.87, 24.74, 24.79, 13900),
(4, '2014-10-29', 24.86, 24.86, 24.78, 24.81, 5500),
(4, '2014-10-28', 24.85, 24.85, 24.80, 24.84, 10600),
(4, '2014-10-27', 24.68, 24.85, 24.68, 24.85, 7700),
(4, '2014-10-24', 24.67, 24.82, 24.59, 24.82, 9300);
Pseudo code for the query would be something like this:
"Find symbols whos most recent closing prices is greater than the closing price 5 trading-days earlier"
The query I'd like to create should result in the following:
Date Symbol Close Close(-5)
2014-11-06 AA 16.37 16.22
2014-11-06 ADT 36.07 35.19
2014-11-06 AEO 13.82 12.86
(the symbol 'AFA' would not match as it's recent close is 24.55 and 5 rows prior it was 24.75)
You can get the price 5 days ago using a correlated subquery. In fact, you can get the most recent price the same way. So, this might be the right path:
select s.*,
(select p.close
from prices p
where p.id = s.id
order by date desc
limit 1
) as Close,
(select p.close
from prices p
where p.id = s.id and p.date <= date(now()) - interval 5 day
order by date desc
limit 1
) as Close_5
from stocks s
having Close > Close_5;