How to select month's fee where month is selected by user - php

I am new to PHP and I am trying to develop a student management system with two tables:
1.student_details_tbl
2.student_fees_tbl
In student_details_tbl all details of students are saved and in the student_fees_tbl table, details of which students have paid their month's fees are saved.
My problem is that I wanted a list of students who have not paid their fees for the user selected month.
student_detais_tbl has the following fields:
id student_id name age address contact image
student_fees_tbl has the following fields:
id student_id month type amount

TRY this query ...
SELECT *
FROM student_detais_tbl
WHERE id NOT IN( SELECT A.student_id
FROM student_fees_tbl A
JOIN student_detais_tbl B
ON A.student_id = B.id AND A.month='Month'
)

SELECT *
FROM student_details_tbl
WHERE student_id NOT IN(SELECT student_id from student_fees_tbl where month='Month')
Is there any problem with this query

Related

Multiple PHP subqueries not giving desired result

I have a MySQL table from which I want to extract attendance information(Student Id, course/subject for attendance, date range,whether the student was present or not). I have written the following query:
SELECT
COUNT(a_id),
(
SELECT COUNT(*) FROM attendance
WHERE state = 'present'
AND `dater` BETWEEN '$a' AND '$b'
) AS Count,
stud_id
FROM attendance
WHERE
stud_id =(SELECT id FROM users WHERE NAME = '$stud')
Which is giving me the correct results, but when I change the student,its not giving me the correct count for the days recorded for present. Not mention that I have not yet added the course parameter into the query
The MySQL table is as follows:
I need help for the query to return the desired results(Count the accurate days present for each student, as well as adding the course parameter into the query so that the query will look for attendance records for a specific course, for a specific student, for a specified date range).
Looks like you want to seperate your queries:
Select (select count(*) from <database>.attendance where state = 'present' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as present, (select count(*) from <database>.attendance where state = 'absent' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as absent from <database>.attendance WHERE stud_id =(SELECT id FROM users WHERE NAME = '$stud');
try this :)
Resolved it using JOIN as follows:
SELECT u.id, a.stud_id, a.course_id, count(*) FROM attendance a
JOIN users u ON u.id=a.stud_id
JOIN courses c ON c.c_id=a.course_id
WHERE a.state='present' and dater between '2017-09-01' and '2017-09-14'
GROUP BY a.stud_id, a.course_id;
Thanks for your help.

MySQL - Group by with 2 conditions

I need some advice with a Select: I need to sum an amount by employee and month. With a Group by I am able to select the Month - the amount will be summed - but not by employee but by month:
SELECT MONTHNAME(datum) AS Monat,ID AS id, STATUS AS STATUS, MA AS Mitarbeiter,SUM(preis) AS Preis
FROM wccrm_prov
GROUP BY MONTHNAME(datum)
The answer could be similar to
SELECT
MONTHNAME(datum) AS Monat
, EMPLOYEE
,ID AS id
,STATUS AS STATUS
,MA AS Mitarbeiter
,SUM(preis) AS Preis
FROM wccrm_prov
GROUP BY MONTHNAME(datum), EMPLOYEE
I used a new field EMPLOYEE, as I'm not familiar with your non-english language used in the question query text. You can obviously swap this with your Employee field

How to compare tabels and get difference out

I have some problems to compare two of my tables. Usually I show my attempt how to solve a problem but here I do not know what to do. Normally, I do it in php. So i get all the information from 2 tables and then compare. But I would like to do it in MySQL. I hope you can help.
The first table, is my transactions table. This is the place where people have used their cards in restaurants.
The second table is my booking table. This is the place where people book reservations in restaurant.
I want to compare these tables, so i can get those who has NOT used their reservation
Transaction mySQL
SELECT t.*, em.* FROM transactions as t
left join exp_members as em on (t.cardid-10000000 = em.member_id)
left JOIN exp_member_data emd on em.member_id = emd.member_id ORDER BY t.created DESC
Transaction Table.
Trans_ID TransactionTime Name Restaurant
1852 2013-04-08 12:45:21 Christian La Canton
1851 2013-04-08 12:41:00 Zaz Parken
Booking mySQL
SELECT b.* from exp_menucard_booking as b;
Booking Table:
ID BookingTime Name NumberOfPeople Restaurant
270 2013-04-09 14:45:00 Christian 2 La Canton
269 2013-04-08 12:17:00 Toby 4 La Raz
As you can se, Toby from Booking table has not used his card (transaction tabel). How can i get him out of my tabel.
If you want to select all persons, who have already booked (there is an entry in booking table) but do not have an entry in the Transaction Table you could try this query:
SELECT *
FROM `Booking` B
WHERE NOT EXISTS (
SELECT *
FROM `Transaction` T
WHERE T.name = B.name
);
BTW it would be better, if you used some User_ID instead of name to identify the persons
This is how you can select all the records that didn't use the booking. It's called an excluding left join or left exclude join. http://www.magecorner.com/mysql-joins-explained/
I'm not sure if this would be faster than WHERE NOT EXISTS, but considering that has a subquery, I think this is faster.
SELECT emb.*
FROM exp_menucard_booking emb
LEFT JOIN transactions t
ON (
t.Name = emb.Name AND
t.Restaurant = emb.Restaurant
)
WHERE t.Trans_ID IS NULL
SELECT * FROM Booking WHERE NAME NOT IN (SELECT NAME FROM Transaction );

How can i retrieve data from a table?

I am working in PHP. This is my query:
$sql = "SELECT *
from place as s
where checkDistance($lati1,$longi1,s.lat,s.lon)<$dist";
This place table has three fields: placeId, PlaceName and Address. Now I want to calculate rating of placeId which are the result of above query. To calculate the rating I have another table rating in which there are two fields: placeId and noOfPerson.
Rating will be calculated by (noOfPerson/maximum_no_of_person) for each placeId. How can i implement this?
Your query could do majority of work here, this will select values from place table joined with number of person for each place, ordered by ranking you need, top-bottom:
SELECT s.placeid, s.PlaceName, s.Address, r.noOfPerson
FROM place as s JOIN rating as r ON (s.placeid = r.placeid)
WHERE checkDistance($lati1,$longi1,s.lat,s.lon)
ORDER BY r.noOfPerson / ( SELECT MAX(noOfPerson) FROM rating ) DESC

Search for Room Availability in Bookings [PHP/MySQL]

I am programming a Hotel Reservation module. Now I have to connect an existing application to get the available rooms on a given range of dates. Please help me in this.
I am here posting the DB schema of the existing app (where the rooms and bookings info were stored).
rooms:
id
room_type
room_count
people
hotel_id
bookings:
id
from
to
name
email
people
dt
hotel_id
bookings_rooms
booking_id
room_id
quantity
I will give 3 inputs
Hotel ID
From Date
To Date
What I need is a list of room_id and max_qty_available
So, rooms aren't actually rooms, they are a type with a count. Does clear up a few things, although by rooms not being seperate entities it is very hard to prevent a change of rooms being eligable.
SELECT r.id, r.room_count - SUM(br.quantity ) AS max_qty_available
FROM rooms r
LEFT JOIN bookings b
ON b.hotel_id = r.hotel_id
AND b.from < <Date END>
AND b.to > <Date START>
LEFT JOIN booking_rooms br
ON br.room_id = r.id
AND br.booking_id = br.id
WHERE r.hotel_id = <Hotel ID>
I`m guessing what some of this parameters are, but here it goes:
Select id as room_id, room_Count as max_qty_available From Rooms Where
id Not in (Select room_id From Bookings_rooms Where booking_id
In (Select id From bookings Where from>=#FromDate And to <=#ToDate And hotel_id=#HotelID)
) And hotel_id = #HotelID
You can replace IN and NOT IN with Exists and Not exists if there is a performance issue. But not sure if Exists are supported in mysql.

Categories