Finding null field with two data tables and date table joined - php

I am trying to figure out how to do this SQL query, but I'm at a loss. I think I am getting in over my head.
I have three tables:
1. datetable, which contains lists of month/year up to year 2020. column is 'thedate'
2. A table containing a list of lectures. A date() field is one column, named "month"
3. A log table, which contains individual rows of data on a month to month basis. i.e Each user has their own row of data for each month of the year.
I would like to take table #2, find if there is a lecture assigned for month x, then take table #3, find the row which corresponds to month x and particular user, then see if column 'hopkins' (a part of table 3) is null or not. I hope this makes a little bit of sense. I've figured out how to use the datetable to find missing rows in table #2, but I haven't figured out how to do this above.
Thanks!

I think you just want a left join from eval to hopkins and then a test on the field:
select e.uid,
max(case when h.name is null then 0 else 1 end) as isInHopkins
from eval e left outer join
hopkins h
on year(e.month) = year(h.month) and
month(e.month) = month(h.month)
group by e.uid;
If the hopkins.id is supposed to related to the user, then you would want to add and e.uid = h.id to the on clause.
If this doesn't give you what you want, then please edit your question to provide sample results for the SQL Fiddle data.

Related

how to create sub queries in MYSQL

excuse me, I'm still confused by the use of sub queries to be implemented in the following table.
I have 2 tables like the following with relation table id_activity
Table 1 for main table
Table 2 for detail table 1
the results I want are as follows
Note*
column date_yesterday, yesterday, interaksi and status are searched from the column today but based on the previous day.
column date_today and today are searched by today
the inprogress and issue columns are taken from the column that contains the value in that column, if the column has no value then it is not included.
I have tried with queries like this, but not according to what I want
SELECT detail_activity.id as id, header_activity.tanggal, detail_activity.today, detail_activity.issue, detail_activity.inprogress, detail_activity.interaksi, detail_activity.status (SELECT tanggal from header_activity left join detail_activity on detail_activity.id_activity = header_activity.id_activity WHERE header_activity.tanggal = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 DAY),"%Y-%m-%d") AND header_activity.id_karyawan = '23') as tanggal_kemarin from header_activity left join detail_activity on detail_activity.id_activity = header_activity.id_activity WHERE header_activity.tanggal = CURDATE() AND header_activity.id_karyawan = '23'
maybe someone can help me to resolve this case
Thanks

How to use colum name as a value and filter highest value MYSQL/PHP

This is my SqlFiddle I have managed to get all values i need except i havent figured out how to check which value from table "tickets" has highest value as column name in table "draws" and then assign value from table "coefficients".
So, if table tickets have values: 9,12,1,11,14,10
And table "draws" have values: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22,21,23,24,25,26,27,28,29,30,31,32,33,1,35
Highest value column name from this "ticket" in table draws is 4th value in table tickets (number 1) and because that is 34th value in table draws i should get 34th value from table "coefficients"
Some PHP or even vb.net function would be ok because i need this for a simple application and it does not matter how it's done as long as it is correct.
Oh, yes... i forgot to put my sql here:
SELECT *,
c.coefficient
FROM tickets t
INNER JOIN draws d ON
(FIELD(t.b1,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b2,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b3,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b4,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b5,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b6,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
INNER JOIN coefficients c
ON c.number = FIELD(t.b6,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)
WHERE draw='1' AND status = '1'
Any help would be awesome.
I think you can use GREATEST() function
try this sqlFiddle

How can I store dates/attendance?

I'm looking for for an opinion.
I have a list of people and will need to store when they are present at a location so those in charge can check them off a list. I'm not 100% sure how long the dates will be needed but I'm assuming they may need to look at previous attendance lists.
My first instinct is to have a column for each date but that could result in many many columns. I could just store a list of dates next to each person:
"01/01/2012,01/15/2012,02/18/2012..."
that could result in a very long entry. It seems like neither is a good option.
If anyone has a suggestion or guidance on an approach please let me know. Thanks.
A complex, but also very clean approach would be
Table "persons":
id
name
Table "dates":
id
location
date
... whatever info the "dates" table needs
Table "attendances":
date_id (link to an entry in the "dates" table)
person_id (link to an entry in the "persons" table)
attended (yes/no)
Then fill the database with the appropriate dates, and fill the "attendances" table according to which persons need to be present at each date.
This is, as said, complex to implement, but it's incredibly flexible - you can have any number of dates and attendees; you can excuse people from attending a specific date programmatically; you can add people to groups...
Link tables.
One table of people
ID
Name
One table of classes
ID
Name
One table linking person to class to date.
ID
personID
classID
cDate
So all you would need to do to determine which students were preset on a certain date in a certain class:
SELECT *
FROM people p
LEFT JOIN peopletoclass ptc ON p.id = ptc.personid
LEFT join class c ON c.id = ptc.classid
WHERE ptc.cDate = '2011-11-07' AND c.id = '1';
Above (for example) would get all people in class id 1 on November 7th 2011.
Create a table "attendance" consisting of a person_id field and a date_present field. You can't store this into columns or a long list using a string ;-).
Than you can use queries where you join the table Person with Attendance.
Your first instinct would result in a horrible table design. What you should have is a seperate table that stores the users/locations/dates tuples
e.g.
userID locationID date
1 party 1/1/2011 00:00:00
1 bathroom 1/1/2011 00:05:00
1 party 1/1/2011 00:15:00
would show that user #1 was at a New Year's Eve party, then went to pray before the porcelain altar at 12:05am, then returned to the party 10 minutes later.

How to set the relative order of columns with a zfdatagrid?

To those of you who are familiar with zfdatagrid for the Zend framework:
I got a recordset by a JOIN query, say from tables s and t, now I want to set the order in which the columns appear in the deployed table.
For example,
5th column of table t to appear at the leftmost side,
then 3rd column of table s,
then 2nd column of table t,
then 4th column of table s.
How do I do this? Whatever I tried, it always shows first the columns of the left table of the JOIN, then the columns of the right table of the JOIN.
I only know how to tell it which columns to show, but not their order.
It may be late but may be useful for others
You can use:
$grid->updateColumn($field,array('position'=>$i))

Select Mulitple Records based on One record's column value

I have a table which contains related records (multiple revisions of the same record). Each record has a string field that resembles a date (date, time, and microtime). I want to select all records that are older than a specific date. If a record has a related record newer than the specific date, I do not want to select any of those related records. Any ideas for that select statement? Eventually, it will be a REMOVE statement.
Edit: Some Sample Rows
id shared_id date type other_data...
1 2 2010-01-01 01:02:03.1234567 original ...
2 3 2010-01-15 11:12:03.1234733 original ...
3 2 2010-02-01 03:04:04.5465654 amendment ...
If my cut-off date was "2010-01-31", I would want to select id #2 only because id #1 has an amendment newer than the cut-off date.
I found this link helping me generate the select statement.
SELECT DISTINCT T.shared_id,T.date,T.id
FROM table T WHERE T.date = (
SELECT MAX( date ) FROM table WHERE shared_id = T.shared_id )
AND T.date < 'my_cut_off_date_string'
This seems to work for me. Thanks for everyone's help.
Maybe you can try the DATEDIFF() function, check this out:
Link 1
Or this one: Link 2
Or maybe you can try the classic query (SELECT FROM table WHERE data <= anotherdata), but in this case you need to convert both data in timestamp format
DELETE from tablename where relatedField = 'relatedValue' AND dateField <= dateToDeleteFrom
Something along those lines should do what you need it to do, if I understand your scenario. If you provide a sample data set I can adjust the statement to more accurately represent your need, but I think this is a good starting point.
HTH,
Jc

Categories