I am trying to manage seasonal prices for hotel rooms.
The only way that I can think of doing it would be to use:
A = Room Rate
B = Service Charge for room
Imagine that the table has a roomId column which is omited from below.
| DayDate |EndDate | A | B
-----------------------------------------------
| 2010/07/1 |2010/07/2 | 200 | 40
| 2010/07/3 |2010/07/4 | 150 | 40
| 2010/07/5 |2010/07/5 | 150 | 50
| 2010/07/6 |2010/07/7 | 200 | 50
| 2010/07/8 |2010/07/9 | 100 | 60
etc.. (table taken from another question).
The problem is: I don't want my seasons to be year specific.
Seasons for rooms shouldn't change year on year. I don't want my users to have to enter the seasonal information several times.
I am also going to have thousands of rooms, so I don't know a way to make this easily manageable.
I'm using mysql and php.
Start with a season table that defines the date ranges for the seasons. It should have a primary key field, say season_id. Then have another table to store room, price and season_id. The season_id is a foreign key to the season table.
Create Table Prices
(
MonthStart int not null
, DayStart int not null
, MonthEnd int not null
, DayEnd int not null
, A int not null
, B int not null
)
Insert Prices( MonthStart, DayStart, MonthEnd, DayEnd, A, B )
Select 7, 1, 7, 2, 200, 40
Union All Select 7, 3, 7, 4, 150, 40
Union All Select 7, 5, 7, 5, 150, 50
Union All Select 7, 6, 7, 7, 200, 50
Union All Select 7, 8, 7, 9, 100, 60
It should be noted that this approach presumes that the boundaries of the seasons are specific to the month and day regardless of year or circumstance. In addition, you'll have to decide how to handle leap year. Another approach which might be simpler is to simply enumerate every day of the year:
Create Table Prices
(
MonthStart int not null
, DayStart int not null
, A int not null
, B int not null
, Constraint PK_Prices Primary Key ( MonthStart, DayStart )
)
Insert Prices( MonthStart, DayStart, A, B )
Select 7, 1, 200, 40
Union All Select 7, 2, 200, 40
Union All Select 7, 3, 150, 40
Union All Select 7, 4, 150, 40
Union All Select 7, 5, 150, 50
Union All Select 7, 6, 200, 50
Union All Select 7, 7, 200, 50
Union All Select 7, 8, 100, 60
Union All Select 7, 9, 100, 60
Related
Here is my table looks..
Users
Id name height weight
1 aaa 1 10
2 bbb 4 104
3 ccc 1 10
4 ddd 56 150
5 eee 232 180
second table looks like
Profile view
Id sender receiver block
1 1 2 True
2 2 3 False
3 4 1 False
The problem i am facing is,,When I search using height and weight in users table and block using profileview table.I couldn't get proper results..
If second user bbb search with height "1" and weight "10" it should be appear 3rd user details ccc .First user also matched but First user blocked second user.The problem is when i using join values coming if sender and receiver exists i the profileview table.If not exist how do we do in joins..
CREATE TABLE #Users
(
Id int,
[Name] varchar(255),
Height int,
[Weight] int
)
CREATE TABLE #ProfileView
(
Id int,
Sender int,
Receiver int,
[Block] varchar(5)
)
INSERT INTO #Users
(Id, [Name], Height, [Weight])
VALUES
(1, 'aaa', 1, 10),
(2, 'bbb', 4, 104),
(3, 'ccc', 1, 10),
(4, 'ddd', 56, 150),
(5, 'eee', 232, 180)
INSERT INTO #ProfileView
(Id, Sender, Receiver, [Block])
VALUES
(1, 1, 2, 'True'),
(2, 2, 3, 'False'),
(3, 4, 1, 'False')
DECLARE
#CallingUser int, --User performing the search
#Height int, --Height searched for
#Weight int --Weight searched for
SET #CallingUser = 2
SET #Height = 1
SET #Weight = 10
SELECT
*
FROM
#Users
LEFT OUTER JOIN #ProfileView
ON #Users.Id = #ProfileView.Id
AND #ProfileView.Receiver = #CallingUser
WHERE
#Users.Id <> #CallingUser
AND #Users.Height = #Height
AND #Users.[Weight] = #Weight
AND (#ProfileView.[Block] = 'False' OR #ProfileView.[Block] IS NULL)
DROP TABLE #Users
DROP TABLE #ProfileView
I have 2 tables
fist one customer
id, name
1, Adam
2, Sam
3, Erik
second one orders
id, father_id , order
1, 1, 1000
2, 1, 2000
4, 2, 4000
5, 3, 600
6, 3 , 433
the php output should be
Adam : orders : 1 - 1000 , 2 - 2000
Sam : orders : 4 - 4000
Erik : orders : 5 - 300 , 6 - 433
How could I do the output in this case using left join method
Im lost
SELECT
c.id,
c.name,
GROUP_CONCAT(CONCAT(o.id,' - ',o.order))
FROM customer c
LEFT JOIN orders o
ON c.id = o.father_id
GROUP BY c.id
I have a table which contains the scores by user, for a game:
UserID (Integer)
MatchId (Integer)
Score (Double)
I'd like to getter sum each user's "points above average" (PAA) - the
amount by which a user's score was above or below the average.
So you'd need to calculate the average of 'Score' for each 'MatchId',
then for each row in the table calculate the amount by which the
'Score' differs from the match average. And then sum that PAA value by
user.
Is it possible to do this via a MySQL query? Or do I need PHP? If it can be done by query, what would that query look like?
plan
compute avg scores by match
join user scores to avg scores and compute sum of derived difference field by userid
setup
create table scores
(
UserID integer not null,
MatchId integer not null,
Score decimal(5, 2) not null,
primary key ( UserID, MatchId )
);
insert into scores
( UserID, MatchId, Score )
values
( 1, 1, 22.1 ),
( 2, 1, 36.0 ),
( 3, 1, 35.3 ),
( 1, 2, 50.0 ),
( 2, 2, 39.8 ),
( 3, 2, 42.0 )
;
query
select s.UserID, sum(s.Score - avgs.avg_score) as paa
from scores s
inner join
(
select MatchId, avg(Score) as avg_score
from scores
group by MatchId
) avgs
on s.MatchId = avgs.MatchId
group by s.UserID
;
output
+--------+-----------+
| UserID | paa |
+--------+-----------+
| 1 | -2.966666 |
| 2 | 0.733334 |
| 3 | 2.233334 |
+--------+-----------+
sqlfiddle
I am having 3 tables in my MySQL DB. The Tables and columns are like in sample below
Table User
id, name, dob, gross_salary
----------------------------------
1, test1, 12 Mar 90, 50000
2, test2, 10 Jan 85, 45000
Table Wage
ida, date, allowence_paid
------------------------------
1, 10 Jul 13, 12000
2, 10 Aug 13, 23000
2, 12 Aug 13, 1000
1, 15 Aug 13, 15000
Table Loan
id, date, loan_amount
--------------------------
2, 05 Jul 13, 500
1, 05 Aug 13, 2500
1, 06 Aug 13, 1200
I need these three tables merged in results for Aug 13 like
id, name, allowence_paid, loan_Amount, paid
--------------------------------------------
1, test1, 15000, 3700, 11300
2, test2, 24000, 0, 24000
SUM of two columns from two different tables joined to another table is required in my case.
Can I get help for the query? I have experimented as per MySQL JOIN with SUM and 3 tables and failed.
This seems to work:
select *, allowence_paid-loan_amount as paid
from
(
select User.id as UserId, User.name as UserName, sum(allowence_paid) as allowence_paid
from Wage join User on Wage.ida=User.id
and Wage.date between '2013-08-01 00:00:00' and '2013-08-31 23:59:00'
group by UserId
) W JOIN
(
select User.id as UserId, User.name as UserName, sum(coalesce(loan_amount,0)) AS loan_amount
from Loan right outer join User on Loan.id=User.id
and Loan.date between '2013-08-01 00:00:00' and '2013-08-31 23:59:00'
group by UserId
) L ON W.UserId = L.UserId
SQL fiddle here.
I have searched a number of different items but I have not found any answers. Chances are I just don't know how to word this correctly.
Anyway, I have set up a system in PHP/SQL to allow instantaneous scanning of thousands of results. Each data entry has 4 numbers, and it can easily scan for entries that match all 4 of these numbers. What I am trying to achieve is to have the script search the database for entries that match exactly 3 out of the 4 entries with the other being incorrect, kind of like a lottery.
For example, we have the entries:
Steve - 1, 4, 10, 13
Bill - 3, 4, 10, 13
Tom - 1, 17, 20, 39
Jill - 1, 4, 13, 21
Jane - 5, 10, 13, 18
Now, I would scan based on the results 1, 4, 10, 13, and would like to return the following results, as these matched 3 of the 4 entries:
Bill - 3, 4, 10, 13
Jill - 1, 4, 13, 21
How would I achieve this?
Many thanks
EDIT: Sorry yes the table has the structure
Name - Number1 - Number2 - Number3 - Number4
So yes, stored as separate fields
You can do this by counting the matches and setting this equal to 3:
select t.*
from t
where (val1 in (1, 4, 10, 13) +
val2 in (1, 4, 10, 13) +
val3 in (1, 4, 10, 13) +
val4 in (1, 4, 10, 13)
) = 3;
In MySQL a TRUE boolean expression evaluates to 1. You can add these together to get the number of matches.