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)
Related
I just want to know one SQL query.
I've got two tables:
id | id_town
1 | 26
id | town
26 | Prague
What's the query if I need to print "Prague"?
Thank you.
PhpMyAdmin prints #1052 - Column 'id' in on clause is ambiguous
I tried this
SELECT town FROM localities JOIN towns ON id = id_town
TRY THIS:
select b.town
from table1 a
inner join table2 b on a.id_town = b.id
I have three tables where table_2 is the middle(connected) between table_1 and table_3
tables
table_id
...
...
table_rest
rest_id
table_id
...
rest
rest_id
...
...
And the query to select I use
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
WHERE rest_id = '$rest_id'
What I need now is to join in this query another table reserv
id
...
status
To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.
EDIT: Sample scenario
tables
table_id
1
2
3
4
5
rest
rest_id
1
2
table_rest
table_id | rest_id
1 2
2 2
3 2
4 2
5 2
So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1
So now I have another table
reserv
id | status
1 0
2 1
3 2
So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.
Hope is a little bit more clear now.
You have to point from table reserv to which table is beign booked, let's call it reserv.table_id
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
left join reserv
on reserv.table_id = m.id
WHERE rest_id = '$rest_id'
and reserv.status is null (*note)
*note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =
It's better if you provide sample data or sqlfiddle. Based on what I realize: Is this what you want:
select tables.table_id, rest.rest_id
from tables
left join table_rest on table_rest.table_id = tables.table_id
left join rest on rest.rest_id = table_rest.rest_id
where rest.rest_id = '$rest_id'
and tables.table_id not in (select id from reserv)
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 .
i am new in php i have a problem in my php code.please help me..
i have two tables
seeker
seeker_nic-----username
111-------------ali
222-------------umer
333-------------raza
`
requestblood
id-------seeker_nic-----requireddate
1------- 111 ----------2012/9/9
2 ------- 222-----------2012/5/8
3 ------ 111-----------2012/10/11
4 ------- 111-----------2012/11/12
5 ------- 222-----------2012/7/9
6 ------- 333 ----------2012/4/4
now i want to list users one time with maximum date like..
s.no---- username----- requireddate
1------- ali---------- 2012/11/12
2------- umer--------- 2012/7/9
3------- raza--------- 2012/4/4
i am using this query
"SELECT bloodrequest.requireddate, seeker.username
FROM
bloodrequest
JOIN seeker ON bloodrequest.seeker_nic= seeker.seeker_nic
Join (SELECT max(requireddate)as maxdate FROM bloodrequest) maxresults on
bloodrequest.requireddate = maxresults.maxdate"
..
but it shows only 1 record not the list
and if use this query (left join instead of join)
"SELECT bloodrequest.requireddate, seeker.username
FROM
bloodrequest
left JOIN seeker ON bloodrequest.seeker_nic = seeker.seeker_nic
left join (SELECT max(requireddate)as maxdate FROM bloodrequest) maxresults
on bloodrequest.requireddate = maxresults.maxdate";
then it shows all records with all dates but not tha maximum..
id------seeker_nic -------requireddate<br>
1 ------ ali --------- 2012/9/9<br>
2 ------ ali ---------- 2012/10/11<br>
3 ------ ali ------------ 2012/11/12<br>
4------ umer------------- 2012/5/8<br>
5------- umer -------------2012/7/9<br>
6 ------ raza--------------2012/4/4<br>
You should be able to do this simpler:
select seeker.username, max(requestblood.requireddate)
from seeker
join requestblood on seeker.seeker_nic=requestblood.seeker_nic
group by seeker.username
Feel free to add any sorting order you need.
Im having the below sql table structure and im trying to grab values from 2 tables using codeigniter.
table: salesman
id sales_name ref_id
1 kevin 174
2 mike 574
3 nick 777
table: sales_report
id salesman_id product purchased_date dispatched
1 2 BF0214 04-November-2011 Yes
2 2 CF0474 09-November-2011 No
3 2 BF0111 10-November-2011 No
4 3 BF0714 15-November-2011 Yes
5 3 BF0435 15-November-2011 Yes
6 2 BF0335 18-November-2011 Yes
7 1 BF0714 22-November-2011 Yes
8 1 BF0335 25-November-2011 Yes
im passing the salesman_id to the model to grab and display the values in my view.
my html table is as below
Ref ID | Salesman Name | Last product Sold | Sold Date | Dispatched Status
the problem that im having how can query to get the sales_name and ref_id from the salesman table and get the most recent product name, dispatched and purchase_date from from the sales_report table?
eg:
Ref ID | Salesman Name | Last product Sold | Sold Date | Dispatched Status
174 kevin BF0335 25-November-2011 Yes
574 mike BF0335 18-November-2011 Yes
777 nick BF0435 15-November-2011 Yes
Use
GROUP BY
ORDER BY
for getting this done
You need to use SELECT values from the sales_report, LEFT JOIN salesman to get in the sales_name and ref_id, ORDER BY the purchased_date (DESC to get the most recent one first instead of the oldest first), and GROUP BY the salesman.id to get it to only return one row per salesman.
SELECT * FROM sales_report LEFT JOIN salesman ON sales_report.salesman_id = salesman.id ORDER BY purchased_date DESC GROUP BY salesman.id
You should find last product by two criteras: its sold date and by its id; because some products may be sold in one day. So, try this one -
SELECT s.ref_id, s.sales_name, sr.product, sr.purchased_date, sr.dispatched FROM salesman s
JOIN (
SELECT salesman_id, product, purchased_date, dispatched FROM (
SELECT salesman_id, product, purchased_date, dispatched, IF(#salesman_id = salesman_id, #i := #i + 1, #i := 1) n, #salesman_id := salesman_id
FROM sales_report, (SELECT #i:= 0, #salesman_id = NULL) vars
ORDER BY salesman_id ASC, purchased_date DESC, id DESC) t
WHERE t.n = 1
) sr
ON s.id = sr.salesman_id;