how to get data of maximum date in php - php

i have three tables..
SEEKER
seeker_nic--- username---request_fulfilled
111-----------ali--------YES
222-----------bilal------YES
second table is
DONOR
donor_nic---username----area
999----------Fahad-------UK
555----------SAJAD------USA
777---------HAMZA-------PK
third table is
STATUS
status-id---seeker_nic---donor_nic--requestfulfilled_by----request_fulfilled_date
1 -------------111-------999---------- Fahad-------------2012/04/09
2 -------------111-------555---------- SAJAD-------------2012/05/15
3--------------222------777-----------HAMZA--------------2012/07/20
now i want this result for SEEKER (111) with latest data..
seeker_nic---username--- request_fulfilled---requestfulfilled_by----area---request_fulfilled_date
111-----------ali--------YES-----------------SAJAD-----------------USA--------2012/05/15
i am trying this query, this query shows rite seeker_nic, and requestfulfilled_date, but it shows wrong donor-nic, area and requestfulfilled_by...
SELECT seeker.seeker_nic, donor.donor_nic, donor.area,
status.requestfulfilled_b , max( status.`request_fulfilled_date` ) AS request_fulfilled_date
FROM seeker
JOIN STATUS ON seeker.seeker_nic = status.seeker_nic
JOIN DONOR ON status.donor_nic = donor.donor_nic
WHERE seeker.username = '$uname'
GROUP BY status.`seeker_nic`
i am getting ans like this....
seeker_nic---username--- request_fulfilled---requestfulfilled_by--------area--------request_fulfilled_date
111-----------ali---------------YES-----------------HAMZA--------------PK------------2012/05/15
plz help me.. :(

Try this:
SELECT seeker.seeker_nic, donor.donor_nic, donor.area, status.requestfulfilled_by, status.request_fulfilled_date
FROM seeker
JOIN (
SELECT seeker_nic, max(request_fulfilled_date) as last_date
FROM status
GROUP BY seeker_nic
) x ON x.seeker_nic = seeker.seeker_nic
JOIN STATUS
ON x.seeker_nic = status.seeker_nic
AND x.last_date = status.request_fulfilled_date
JOIN DONOR
ON status.donor_nic = donor.donor_nic
WHERE seeker.username = '$uname'

If you need to select latest date for one particular user, you do not need a GROUP BY clause:
SELECT
status.request_fulfilled_date, # status.requestfulfilled_by,
seeker.seeker_nic, seeker.username, seeker.request_fulfilled,
donor.donor_nic, donor.username, donor.area
FROM status
LEFT JOIN seeker ON status.seeker_nic = seeker.seeker_nic
LEFT JOIN donor ON status.donoc_nic = donor.donor_nic
WHERE seeker.username = 'ali'
ORDER BY status.request_fulfilled_date DESC
LIMIT 1

SELECT x.seeker_nic, y.username, y.request_fulfilled,
x.requestfulfilled_by, z.area, x.request_fulfilled_date
FROM status x, seeker y, donor z
WHERE x.seeker_nic=y.seeker_nic AND y.seeker_nic=111 AND z.donor_nic=x.donor_nic;

Related

I want the highest value to be on the top of the list

So I have this query and display the value, the 'time' is integer, it is often updated by clicking and when it is updated I want the highest value si on the top of the list when page is refresh, but it's not, I wonder if the grouping affects the query?
$sql= "select * FROM message
where userid_to = '$UserLoggedIn' || userid_from = '$UserLoggedIn'
group by conversation_id
ORDER BY time DESC";
I think you should be doing the aggregation on conversations inside a subquery to find the most recent time for each conversation. Then join back to your message table to obtain the full message record. I'm not sure what went wrong with your ordering, but your current query is not deterministic.
SELECT m1.*
FROM message m1
INNER JOIN
(
SELECT conversation_id, MAX(time) AS max_time
FROM message
WHERE userid_to = '$UserLoggedIn' OR userid_from = '$UserLoggedIn'
GROUP BY conversation_id
) m2
ON m1.conversation_id = m2.conversation_id AND
m1.time = m2.max_time
ORDER BY
m1.time DESC;
I would try doing it this way:
SELECT *, MAX(time) as time
FROM message
WHERE userid_to = '$UserLoggedIn' OR userid_from = '$UserLoggedIn'
GROUP BY conversation_id;

Combining two SQL queries PDO

I'm quite stuck on the following issue. I have a series of tables:
What I want to do is get all the information on a room, assuming that the amount of bookings don't exceed the room number available for that Room.
So to get my Room details my SQL is this:
SELECT Rooms.RoomID as RoomID,
RoomName, NumOfRooms,
MaxPeopleExistingBeds,
MaxExtraBeds,
MaxExtraPeople,
CostPerExtraPerson,
MaximumFreeChildren,
IncludeBreakfast,
MinRate
FROM Rooms, RoomDetails
WHERE Rooms.AccommodationID = :aid AND
Rooms.RoomID = RoomDetails.RoomID
GROUP BY RoomName
Which upon return gets me a list of details for those rooms as follows:
I then use this query to get the number of bookings, and the ID of the room:
SELECT Booking.RoomID,
count(Booking.RoomID) as Bookings
FROM Booking
WHERE ArriveDate >= :aDate AND
DepartDate <= :dDate AND
AccommodationID = :aid
GROUP BY RoomID
I then combine both and feed the two arrays back in one array using this function:
public function get_availability($aid, $aDate, $dDate) {
$stmt = $this->db->prepare('SELECT Rooms.RoomID as RoomID, RoomName, NumOfRooms, MaxPeopleExistingBeds, MaxExtraBeds, MaxExtraPeople, CostPerExtraPerson, MaximumFreeChildren, IncludeBreakfast, MinRate FROM Rooms, RoomDetails WHERE Rooms.AccommodationID = :aid AND Rooms.RoomID = RoomDetails.RoomID GROUP BY RoomName');
$stmt->bindValue(':aid', $aid);
$stmt->execute();
$rooms = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt2 = $this->db->prepare('SELECT Booking.RoomID, count(Booking.RoomID) as Bookings FROM Booking WHERE ArriveDate >= :aDate AND DepartDate <= :dDate AND AccommodationID = :aid GROUP BY RoomID');
$stmt2->bindValue(':aid', $aid);
$stmt2->bindValue(':aDate', $aDate);
$stmt2->bindValue(':dDate', $dDate);
$stmt2->execute();
$bookings = $stmt2->fetchAll(PDO::FETCH_ASSOC);
$room = array($rooms, $bookings);
return (!empty($room)) ? $room : false;
}
The thing is, what I actually want to do is only return the room details where NumOfRooms is less than the number of Bookings.
So for instance where I have $bookings, if it tells me that for room ID 4, I have 3 bookings for a set period, and my NumOfRooms is 1. Then I know that I have no capacity that week to take any more bookings on. If however I have 1 booking and one capacity then that is still full. But if I have NumOfRooms of 2, and bookings amount to 1, I know I have room.
So basically if NumOfRooms > BookingCount then the room is available.
How can I amalgamate both queries and simplify my code to make this possible?
I.E to put it simply, how do I select all of the info from RoomDetails given an ArriveDate in Booking and a DepartDate and a RoomID, where NumOfRooms > count(Booking.RoomID) (Where it is within those dates and the room id is equal to the room id of Rooms).
Your problem can be solved by simply updating the SQL statement itself:
SELECT r.RoomID AS RoomID,
RoomName,
NumOfRooms,
MaxPeopleExistingBeds,
MaxExtraBeds,
MaxExtraPeople,
CostPerExtraPerson,
MaximumFreeChildren,
IncludeBreakfast,
MinRate
FROM Rooms r
JOIN RoomDetails rd
ON r.RoomID = rd.RoomID
JOIN (
SELECT b.RoomID,
AccommodationID,
count(b.RoomID) AS Bookings
FROM Booking b
WHERE ArriveDate >= :aDate
AND DepartDate <= :dDate
GROUP BY RoomID
) t
ON t.AccommodationID = r.AccommodationID
WHERE r.AccommodationID = :aid
AND t.Bookings < NumOfRooms
GROUP BY RoomName
You can select out all of the booking counts per room for the desired date range as a subquery, and then LEFT JOIN that subquery against the list of your rooms filtered by your desired AccommodationID and the desired NumOfRooms > BookingCount criteria. The key here is in the join type used for this subquery, as an inner join would limit your results to only rooms that actually had bookings.
SELECT Rooms.RoomID as RoomID,
RoomName, NumOfRooms,
MaxPeopleExistingBeds,
MaxExtraBeds,
MaxExtraPeople,
CostPerExtraPerson,
MaximumFreeChildren,
IncludeBreakfast,
MinRate,
BookingCount
FROM Rooms
INNER JOIN RoomDetails on Rooms.RoomID = RoomDetails.RoomID
LEFT JOIN (
SELECT Booking.RoomID,
count(Booking.RoomID) as BookingCount
FROM Booking
WHERE ArriveDate >= :aDate AND
DepartDate <= :dDate
GROUP BY Booking.RoomID
) RoomBookings ON Rooms.RoomID = RoomBookings.RoomID
WHERE Rooms.AccommodationID = :aid
AND NumOfRooms > BookingCount
GROUP BY RoomName

MySQL returning value only for ID 1

I got the following query:
SELECT ur.repair_id, ur.repair_complete, ur.repair_noted_by_client, ur.repair_problem, ur.added_at, ur.added_by, ur.repaired_at, ur.repaired_by,
aa.account_fullname AS added_by_name,
ar.account_fullname AS repaired_by_name
FROM units_repairs AS ur
LEFT JOIN (SELECT account_id, account_fullname FROM accounts LIMIT 1) AS aa ON aa.account_id = ur.added_by
LEFT JOIN (SELECT account_id, account_fullname FROM accounts LIMIT 1) AS ar ON ar.account_id = ur.repaired_by
WHERE ur.unit_id = 1
It return the fullname only if the account_id = 1. If let say repaired_by = 2 then it say NULL...
Thanks, I don't know what I am missing.
You are joining with inner query:
SELECT account_id, account_fullname FROM accounts LIMIT 1
where you have LIMIT 1, which gives you only one row (which probably has repaired_by = 1), and then you want to filter and get only rows where repaired_by is 2...and you don't have that one.
Probably there are no records that satisfy the condition repaired_by = 2 in the line,
LEFT JOIN (SELECT account_id, account_fullname FROM accounts LIMIT 1) AS ar ON ar.account_id = ur.repaired_by
Hence, ar.account_id = ur.repaired_by returns NULL.

MySQL Query for Team Schedule

I am trying to retrieve schedules for each team based on my table below, but I am having some trouble getting the team names to display. Here are my tables:
TEAM
(team_id,
team_name,
team_mascot,
etc.)
GAME
(game_id,
game_date,
game_home_team,
game_visitor_team,
game_home_score,
game_visitor_score,
game_complete)
Here is my current query. I am able to get the team's id values with this but I need the team names as id values won't do me much good.
SELECT game_home_team, game_visitor_team, game_date
FROM game
INNER JOIN team home ON home.team_id = game_home_team
INNER JOIN team visitor ON visitor.team_id = game_visitor_team
WHERE game_home_team = ?
OR game_visitor_team = 6
ORDER BY game_date ASC;
How can I retrieve the team names with this query and also display a result such as W or L depending on the score for the game? Thanks so much in advance.
SELECT
home.team_name AS home_team,
visitor.team_name AS visitor_team,
game_date,
IF(
game_home_score = game_visitor_score,
'Tie',
IF(
game_home_score > game_visitor_score,
'Hometeam',
'Visitorteam'
)
) AS Winner
FROM game
INNER JOIN team home ON home.team_id = game_home_team
INNER JOIN team visitor ON visitor.team_id = game_visitor_team
WHERE game_home_team = ?
OR game_visitor_team = 6
ORDER BY game_date ASC;
select game_home_team, home.team_name AS homename,
game_visitor_team, visitor.team_name AS visitorname
etc...

Join two mysql tables in a query?

My aim is to prepare a newsletter which sends all my users an email when they haven't logged in for longer than two weeks.
To accomplish this, I need to "join" (I think) the last logged in date (from myMembers table) and their preference in receiving emails or not (from accountOptions table).
myMembers
===============
id last_logged_date
1 date
2 date
accountOptions
===============
id uid notification1
1 1 yes
2 2 no
What is the query I need to use here to extract th?
select *
from myMembers m, accountOptions o
where u.notification='yes' AND m.id = o.uid
AND AND DATEDIFF(Now(), m.last_logged_date) > 14
DateDiff(date1, date2) returns the difference between these two dates in days.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
assuming id as the reference
select aO.id, aO.uid, m.last_logged_date from accountOptions aO inner join myMembers m on
aO.id = m.id where m.last_logged_date <= DATEDIFF(Now(), m.last_logged_date) > 14
and notification1 = 'yes'
SELECT myMembers.last_logged_date, accountOptions.notification1
FROM myMembers
INNER JOIN accountOptions
ON myMembers.P_Id=accountOptions.uid
ORDER BY myMembers.id
Use this query to join 2 tables
SELECT *
FROM myMembers,accountOptions
WHERE myMembers.id = accountOptions.uid
AND id=1;
This will return the exact result.

Categories