MYSQL select query giving duplicated listing - php

I need some help with this query. I have 2 tables of data
Booking table
bookingid
booking_date
booking_start
staffid
studentid
status
1
2021-10-10
7.30pm
1
12345678
ended
2
2021-10-10
11.30am
1
12345679
ended
3
2021-10-10
12.00pm
1
NULL
cancelled
Student table
|studentid|firstname|lastname|
|--|--|--|
|12345678|john|doe|
|12345679|mary|doe|
|12345670|vincent|doe|
What table im looking for
booking_date
booking_start
studentname
2021-10-10
7.30pm
john doe
2021-10-10
11.30pm
mary joe
2021-10-10
12.00pm
NULL
Using this query,
Select Booking_date,
Booking_start,
case WHEN booking.StudentID is NULL THEN NULL ELSE student.First_name end as First_name,
case WHEN booking.StudentID is NULL THEN NULL ELSE student.Last_name end as Last_name,
BookingID
from booking, student
where (booking.staffid = '$userid') ORDER BY booking_start ASC)
This is the table i am getting
booking_date
booking_start
studentname
2021-10-10
7.30pm
john doe
2021-10-10
7.30pm
mary doe
2021-10-10
7.30pm
vincent doe
2021-10-10
11.30pm
mary joe
2021-10-10
11.30pm
john joe
2021-10-10
11.30pm
vincent joe
2021-10-10
12.00pm
2021-10-10
12.00pm
There should be 1 more 2021-10-10|12.00pm|| im unable to show it due to formatting issues.
It shows duplicated listing with the wrong student name.
What can I do to fix this query?

You need to join tables like this :
booking inner join student on booking.StudentID = student.StudentID
Select Booking_date,
Booking_start,
student.First_name,
student.Last_name,
BookingID
from booking inner join student on booking.StudentID = student.StudentID
where (booking.staffid = '$userid')
ORDER BY booking_start ASC
if you dont join tables, they act like cartesian joins, all rows are matched with all others, so it generates duplicate . for example , if you have 2 table with 10 rows each, in result you get 10*10 result ...
Inner join = you get only if records are matched.
If you have records on booking table and it has no studing_id matched with students table , and still want to display it as a result, you need to use LEFT JOIN instead of INNER JOIN
Also its the best to use join syntax instead of old joins like :
old syntax :
student,booking
where
student.id = booking.studentid
new syntax:
student inner join booking on student.id = booking.studentid

Related

How to join two not too related tables

I'm really having difficulty doing a join? How can i join two table that the only relation is the username. For example:
I have two tables. tableone and tabletwo each with there own respective rows and columns.
Table One
id trans_ref username amount
2 12345 peter 50
3 45678 john 30
4 8790 frank 10
Table Two
id trans_ref username recurring status company date_order amt
1 78987 peter weekly paid new lad 12/10/2015 30
2 88776 john monthly unpaid green 15/05/2015 10
3 55667 frank yearly paid blue 17/05/2015 25
how do i perform a join so that all the values will be avail to me
$stm = $pdo->....
while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
echo $row['status']; //etc
}
Since both tables have a trans_ref column, you'll need to give at least one of them an alias so you can access it distinctly from the other.
SELECT t1.trans_ref AS t1_trans_ref, t1.amount, t2.*
FROM table1 AS t1
JOIN table2 AS t2 ON t1.username = t2.username
$row['trans_ref'] will be the trans_ref column from table2, $row['t1_trans_ref'] will be the trans_ref column from table1.

data from two tables without join

Table "subjects" contains columns:
sub_date, sub_content, student_id
2014-10-03, english, 1
2014-10-09, maths, 2
2014-10-11, biology, 1
and
Table games contains columns:
game_date, game_content, student_id
2014-10-05, Hockey, 1
2014-10-18, Tennis, 1
2014-10-20, Cricket, 2
I want to display all details of student_id 1 order by date (considering both sub_date and game_date
Date, Topic
2014-10-03, english
2014-10-05, Hockey
2014-10-11, biology
2014-10-18, Tennis
Please help
you could try this:
select sub_date,sub_content from subjects where student_id = 1
UNION
select game_date,topic from games where student_id = 1
You can try with this (even though sounds not such as a good idea):
SELECT tmp.sub_date AS Date,tmp.sub_content AS Topic
FROM (
SELECT sub_date,sub_content,student_id FROM subjects
UNION SELECT game_date,game_content,student_id FROM games
) tmp
WHERE tmp.student_id = 1 ORDER BY tmp.sub_date;

Return all the rows even the joined table has empty results

In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .

MySQL selecting row for attendance case

I have these tables:
table 1 : attendance
-------------------------------
ID | DATE | EMPLOYEE_ID |
-------------------------------
1 2013-09-10 1
2 2013-09-10 2
3 2013-09-10 3
-------------------------------
table 2: employee
---------------
ID | NAME |
---------------
1 Smith
2 John
3 Mark
4 Kyle
5 Susan
6 Jim
---------------
My actual code to show employee option.
while($row=mysql_fetch_array($query)){
echo "<option value='$row[employee_id]'>$row[first_name] $row[last_name]</option>";
}
?>
How can i show the list of employee that not registered in table 1?
The condition is if an employee already registered in table 1, they won't appear on the option.
I want to show the list in <option>'s of <select> element. So it will return: kyle, susan, jim.
Please tell me the correct query or if there is any better option, it'll be good too. Please give some solution and explain. Thank you very much
UPDATE / EDIT:
it also based on current date, if in table 1 have no latest date e.g. today it's 2013-09-15. It will show all of employee.
You can do this with a left join and then checking for no matches:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id
where a.employee_id is null;
This is probably the most efficient option in MySQL.
EDIT:
To include a particular date, add the condition to the on clause:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id and a.date = date('2013-09-20')
where a.employee_id is null;
If I understood correctly this should work, get all employees whose ID is not in the attendance table.
SELECT * FROM employee WHERE employee.ID NOT IN
(SELECT EMPLOYEE_ID FROM attendance)

mysql join 2 user_ids in a row from users table

There are 2 tables:
orders:
id requester_id supplyer_id status
1 423 1 reserved
2 500 1 supplied
3 222 2 reserved
...
users
id username register_date
1 admin 2012-01-01
2 smith 2013-01-01
...
423 John 2012-10-11
500 Doe 2012-12-11
222 name 2012-10-13
...
I want to join these two tables and get this as a result
id requester_username supplier_username status
1 John admin reserved
2 Doe admin supplied
3 name smith reserved
I can actually join these tables using active records like :
$this->db->select('orders.*,users.username')
->from('orders')
->join('users','users.id = orders.requester_id')
->get()->result();
but I dont know how to get the supplier username at the same time.
Try adding a second JOIN:
$this->db->select('orders.*,requesters.username,suppliers.username')
->from('orders')
->join('users AS requesters', 'requesters.id = orders.requester_id')
->join('users AS suppliers', 'suppliers.id = orders.supplier_id')
->get()->result();
In MySql Your query should be like -
Select a.id, b.username, c.username, a.status
from orders a, users b, users c
where
a.requester_id = b.id
And
a.supplyer_id = c.id
Thanks

Categories