I'm doing a section on my website called "My Ideal Surfboard", the user enters their data (weight, height, etc.), a comparison is made in database (join) and is returned to the ideal types of surfboard user according to his profile.
I have a table with the reference of all sizes and types of surfboard according to height, weight and user experience.
I'm doing the following:
Divided into two tables:
- Table USER obviously stores user data (experience, height and weight);
- Table SURFBOARD possesses the reference values (type, size, weight and litres) on each surfboardd according to experience, weight, height of the user.
-> I liken the table USER to the table SURFBOARD and return to the user the ideal model. How to do this?
At first I thought putting the same fields both in the table USER as in the table SURFBOARD make a inner join and have the data you want.
However, the both tables would duplicate values.
`dados_usuario` `prancha`
height2 weight2
height2 weight2
height2 weight2
height1 weight2
height1 weight2
height1 weight2
I compare and I display...
I believe that this is not a good practice and not the best way to do this. I know there are other methods to do this.
The issue is, how best way to compare these data?
How to identify which line is compatible with the data that the user will enter?
MY DATABASE:
CREATE TABLE USER(
usuario INT NOT NULL AUTO_INCREMENT,
nome VARCHAR(150) not null,
email VARCHAR(50) not null,
estilo VARCHAR(14) not null,
exp VARCHAR(13) not null,
altura VARCHAR(12) not null,
peso VARCHAR(9) not null,
PRIMARY KEY(usuario)
);
CREATE TABLE SUFBOARD(
prancha_pri INT NOT NULL AUTO_INCREMENT,
tipo_prancha VARCHAR(9) NOT NULL,
tamanho_prancha VARCHAR(9) not null,
meio_prancha VARCHAR(12) not null,
litragem_prancha VARCHAR(8) not null,
PRIMARY KEY (prancha_pri)
);
INSERTING DATA IN TABLE 'USER':
INSERT INTO EXPERIENCIA VALUES (NULL, 'joao', 'a#a.com', 'Surf', 'INICIANTE', '<1,60m', '>90kg');
INSERT INTO EXPERIENCIA VALUES (NULL, 'john', 'b#b.com', 'StandUP Paddle', 'INTERMEDIARIO', '1,81 - 1,90m', '81 - 90kg');
INSERT INTO EXPERIENCIA VALUES (NULL, 'carl', 'c#c.com', 'Surf', 'AVANÇADO', '>1,90m', '71 - 80kg');
INSERTING DATA IN TABLE SURFBOARD:
INSERT INTO PRANCHA VALUES (1, 'FUN', '8', '21 polegadas', '43L');
INSERT INTO PRANCHA VALUES (2, 'FUN', '8.8', '21 polegadas', '43L');
INSERT INTO PRANCHA VALUES (3, 'LONGBOARD', '9.2', '21 polegadas', '55L');
INSERT INTO PRANCHA VALUES (4, 'PRANCHA', '5.5 a 5.8', '20 polegadas', '30L');
INSERT INTO PRANCHA VALUES (5, 'PRANCHA', '5.5 a 5.10', '20 polegadas', '30L');
INSERT INTO PRANCHA VALUES (6, 'PRANCHA', '5.9 a 6.0', '21 polegadas', '32L');
INSERT INTO PRANCHA VALUES (7, 'PRANCHA', '6.0 a 6.4', '21 polegadas', '34L');
INSERT INTO PRANCHA VALUES (8, 'PRANCHA', '5.10 a 6.4', '20 polegadas', '30L');
INSERT INTO PRANCHA VALUES (9, 'PRANCHA', '5.10 a 6.4', '20 polegadas', '32L');
INSERT INTO PRANCHA VALUES (10, 'PRANCHA', '6.2 a 6.6', '21 polegadas', '32L');
INSERT INTO PRANCHA VALUES (11, 'PRANCHA', '6.4 a 6.8', '21 polegadas', '34L');
INSERT INTO PRANCHA VALUES (12, 'PRANCHA', '6.2 a 6.6', '20 polegadas', '30L');
INSERT INTO PRANCHA VALUES (13, 'PRANCHA', '6.2 a 6.6', '21 polegadas', '30L');
INSERT INTO PRANCHA VALUES (14, 'PRANCHA', '6.2 a 6.6', '21 polegadas', '34L');
INSERT INTO PRANCHA VALUES (15, 'PRANCHA', '6.2 a 6.6', '21 polegadas', '36L');
INSERT INTO PRANCHA VALUES (16, 'PRANCHA', '6.2 a 6.6', '21 polegadas', '38L');
INSERT INTO PRANCHA VALUES (17, 'PRANCHA', '6.2 a 7.0', '21 polegadas', '34L');
INSERT INTO PRANCHA VALUES (18, 'PRANCHA', '6.2 a 7.0', '21 polegadas', '38L');
INSERT INTO PRANCHA VALUES (19, 'PRANCHA', '5.5 a 5.8', '18 polegadas', '23L');
INSERT INTO PRANCHA VALUES (20, 'PRANCHA', '5.8 a 5.10', '18 polegadas', '24L');
INSERT INTO PRANCHA VALUES (21, 'PRANCHA', '5.10', '18 polegadas', '27L');
INSERT INTO PRANCHA VALUES (22, 'PRANCHA', '6.0 a 6.2', '19 polegadas', '28L');
INSERT INTO PRANCHA VALUES (23, 'PRANCHA', '6.0 a 6.2', '19 polegadas', '29 a 31L');
INSERT INTO PRANCHA VALUES (24, 'PRANCHA', '5.10 a 6.0', '19 polegadas', '24L');
INSERT INTO PRANCHA VALUES (25, 'PRANCHA', '5.10', '19 polegadas', '26L');
INSERT INTO PRANCHA VALUES (26, 'PRANCHA', '6.0', '19 polegadas', '27L');
INSERT INTO PRANCHA VALUES (27, 'PRANCHA', '6.0', '19 polegadas', '29L');
INSERT INTO PRANCHA VALUES (28, 'PRANCHA', '6.2', '20 polegadas', '30 a 31L');
INSERT INTO PRANCHA VALUES (29, 'PRANCHA', '6.0', '19 polegadas', '25L');
INSERT INTO PRANCHA VALUES (30, 'PRANCHA', '6.0', '19 polegadas', '28L');
INSERT INTO PRANCHA VALUES (31, 'PRANCHA', '6.0', '19 polegadas', '30L');
INSERT INTO PRANCHA VALUES (32, 'PRANCHA', '6.0 a 6.2', '20 polegadas', '30 a 31L');
INSERT INTO PRANCHA VALUES (33, 'PRANCHA', '5.11', '19 polegadas', '26L');
INSERT INTO PRANCHA VALUES (34, 'PRANCHA', '5.11', '19 polegadas', '28L');
INSERT INTO PRANCHA VALUES (35, 'PRANCHA', '6.0', '20 polegadas', '29L');
INSERT INTO PRANCHA VALUES (36, 'PRANCHA', '6.1', '20 polegadas', '30L');
INSERT INTO PRANCHA VALUES (37, 'PRANCHA', '6.1 a 6.6', '20 polegadas', '30 a 31L');
INSERT INTO PRANCHA VALUES (38, 'PRANCHA', '6.1', '19 polegadas', '27L');
INSERT INTO PRANCHA VALUES (39, 'PRANCHA', '6.1', '19 polegadas', '28L');
INSERT INTO PRANCHA VALUES (40, 'PRANCHA', '6.1 a 6.3', '20 polegadas', '29L');
INSERT INTO PRANCHA VALUES (41, 'PRANCHA', '6.1 a 6.4', '20 polegadas', '31L');
INSERT INTO PRANCHA VALUES (42, 'PRANCHA', '6.2 a 6.6', '20 polegadas', '31L');
In my form, visually they are all of height and weight exact values. However, the value of the fields are the values of my reference table:
HEIGHT:
<option value="1,71 - 1,80m">1.71m</option>
<option value="1,71 - 1,80m">1.72m</option>
<option value="1,71 - 1,80m">1.73m</option>
<option value="1,71 - 1,80m">1.74m</option>
<option value="1,71 - 1,80m">1.75m</option>
<option value="1,71 - 1,80m">1.76m</option>
WEIGHT:
<option value="81 - 90kg">88Kg</option>
<option value="81 - 90kg">89Kg</option>
<option value="81 - 90kg">90Kg</option>
<option value=">90kg">91Kg</option>
<option value=">90kg">92Kg</option>
<option value=">90kg">93Kg</option>
<option value=">90kg">94Kg</option>
First you have to tell us, given a user and some boards, what makes the "ideal" board(s). Then that gets translated to SQL.
PS
You are going to have to remove units from your values if you want your queries to talk about those values using comparisons or arithmetic. (And then you can store numbers using numerical types.) You are going to have to store ranges as pairs of columns if you want to mention their endpoints easily. Otherwise you will have to write things like
U.HEIGHT >= get_min_from_range_as_number(B.HEIGHT)
U.WEIGHT <= get_weight_as_number_without_units(B.WEIGHT)
where the functions are complex.
Say you want rows where:
user USERID has ideal board(s) BOARDID
You have to tell us what that means in terms of simpler things. Eg perhaps it means that:
(read U.ID as USERID, B.ID as BOARDID)
there exist values for U.NAME, U.HEIGHT, ..., B.WEIGHT where
user U.ID with name U.NAME ... has height U.HEIGHT...
AND board B.ID suits height between B.MINHEIGHT and B.MAXHEIGHT ...
AND U.HEIGHT >= B.MINHEIGHT AND U.HEIGHT <= B.MAXHEIGHT
AND (B.MINHEIGHT + B.MAXHEIGHT)/2 <= U.WEIGHT * 100
AND ...
OR ...
Now we need a query that returns the rows that make that statement template into a true statement.
Already User holds rows where:
user ID with name NAME ... has height HEIGHT ...
And Board holds rows where:
board ID suits height between MINHEIGHT and MAXHEIGHT ...
But the nature of SQL JOIN is that table1 t1JOINtable2 t2 holds the rows that satisfy the first table's statement template ANDed to the second's with parameters/columns prefixed by aliases and dots. So User U JOIN Board B holds rows where:
user U.ID with name U.NAME ... has height U.HEIGHT ...
AND board B.ID suits height between B.MINHEIGHT and B.MAXHEIGHT ...
And the nature of WHERE is that tableWHEREcondition holds the rows that satify table's statement template ANDed with condition. So
User U JOIN Board B
WHERE U.HEIGHT >= B.MINHEIGHT AND U.HEIGHT <= B.MAXHEIGHT
...
holds rows where:
user U.ID with name U.NAME ... has height U.HEIGHT ...
AND board B.ID suits height between B.MINHEIGHT and B.MAXHEIGHT ...
AND U.HEIGHT >= B.MINHEIGHT AND U.HEIGHT <= B.MAXHEIGHT
...
Then SELECT drops any unwanted parameters/columns. So user U.ID has ideal oard(s) B.ID holds rows where:
SELECT U.ID, B.ID
FROM User U JOIN Board B
WHERE U.HEIGHT >= B.MINHEIGHT AND U.HEIGHT <= B.MAXHEIGHT
...
SELECT also renames columns. So to get our overall query of the rows where user USERID has ideal board(s) BOARDID we need:
SELECT U.ID AS USERID, B.ID AS BOARDID
FROM User U JOIN Board B
WHERE U.HEIGHT >= B.MINHEIGHT AND U.HEIGHT <= B.MAXHEIGHT
...
Related
I have a table that contains properties and their types:
INSERT INTO properties (property_id, year_built, type_id, code)
VALUES
(1, 2000, 3, 'ABC'),
(2, 2001, 3, 'ABC'),
(3, 2002, 3, 'ABC'),
(4, 2003, 3, 'ABC'),
(5, 2004, 3, 'ABC'),
(6, 2005, 3, 'ABC'),
(7, 2000, 3, 'DEF'),
(8, 2001, 3, 'DEF'),
(9, 2002, 3, 'DEF'),
(10, 2003, 3, 'DEF'),
(11, 2004, 3, 'DEF'),
(12, 2005, 3, 'DEF'),
(13, 2000, 3, 'GHI'),
(14, 2001, 3, 'GHI'),
(15, 2002, 3, 'GHI'),
(16, 2003, 3, 'GHI'),
(17, 2004, 3, 'GHI'),
(18, 2005, 3, 'GHI');
I have a second table 'agents' with the same number of records as the properties table.
INSERT INTO agents (agent_id, year_built, type_id)
VALUES
(50, 2000, 3),
(51, 2001, 3),
(52, 2002, 3),
(53, 2003, 3),
(54, 2004, 3),
(55, 2005, 3),
(56, 2000, 3),
(57, 2001, 3),
(58, 2002, 3),
(59, 2003, 3),
(60, 2004, 3),
(61, 2005, 3),
(62, 2000, 3),
(63, 2001, 3),
(64, 2002, 3),
(65, 2003, 3),
(66, 2004, 3),
(67, 2005, 3);
There is a field in the properties table: 'agent_id' that should be populated with a single agent of the same year and type. For example, this is the expected result of the properties table for the year 2000 after running an update statement:
SELECT (*) FROM properties WHERE year_built = 2000;
property_id year_built type_id code agent_id
1 2000 3 ABC 50
7 2000 3 DEF 56
13 2000 3 GHI 62
Every join that I try results in all matching agent records returned for each property_id. For example:
SELECT properties.*, agents.agent_id
FROM properties
JOIN agents
USING(year_built, type_id)
WHERE properties.year_built = 2000;
Would give the result:
property_id year_built type_id code agent_id
1 2000 3 ABC 50
1 2000 3 ABC 56
1 2000 3 ABC 62
7 2000 3 DEF 50
7 2000 3 DEF 56
7 2000 3 DEF 62
13 2000 3 GHI 50
13 2000 3 GHI 56
13 2000 3 GHI 62
I'm aware that a simple join will return all the agent records, but I'm not sure how to match a single agent record to a single properties record with just the fields I have to work with. In addition, I would want these to be ordered - so that the first property_id for a year/type matches with the first agent_id of the same year/type. I should also add that neither table's fields, keys, or properties can be modified.
As the data from table properties can be evenly matched to the data from table agents, we can capitalize on the row number added to each table for precise matching. This is written and tested in workbench using MySQL5.7 :
select p.property_id,p.year_built,p.type_id,p.code,agent_id from
(select property_id,year_built,type_id,code,#row_id:=#row_id+1 as rowid
from properties,(select #row_id:=0) t ) p
join
(select agent_id,year_built,type_id,#row_number:=#row_number+1 as rownumber
from agents,(select #row_number:=0) t ) a
on p.year_built=a.year_built and p.type_id=a.type_id and p.rowid=a.rownumber
where p.year_built=2000
;
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);
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
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.
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;