Question

I have following MySQL Tables:

+--------------------+----------+------+-----+---------+----------------+
| Field              | Type     | Null | Key | Default | Extra          |
+--------------------+----------+------+-----+---------+----------------+
| id                 | int(11)  | NO   | PRI | NULL    | auto_increment |
| firstName          | longtext | NO   |     | NULL    |                |
| lastName           | longtext | NO   |     | NULL    |                |
| username           | longtext | NO   |     | NULL    |                |
+--------------------+----------+------+-----+---------+----------------+

And:

+------------------------+----------+------+-----+---------+----------------+
| Field                  | Type     | Null | Key | Default | Extra          |
+------------------------+----------+------+-----+---------+----------------+
| id                     | int(11)  | NO   | PRI | NULL    | auto_increment |
| trainee_id             | int(11)  | NO   | MUL | NULL    |                |
| date                   | date     | NO   |     | NULL    |                |
| duration               | time     | NO   |     | NULL    |                |
| educationDepartment    | longtext | NO   |     | NULL    |                |
| completedtasks         | longtext | NO   |     | NULL    |                |
+------------------------+----------+------+-----+---------+----------------+

I have two conditions. I have a start and a end-date from my second table and I want the data from a specific user which I just can verify through the "username" of the first table. It looks like this: username is known > I will get the id (Foreign Key) of the user > now I can use this Foreign Key to get userspecific data from the second table. I started my MySQL query and it looks like this atm:

SELECT date, duration, educationDepartment, completedtasks FROM programm_completedtask WHERE date BETWEEN "2014-04-07" AND "2014-04-11";

But how can I make it now user specific ? IMPORTANT: I just want a MySQL query, php or something like that won't help me :/

Was it helpful?

Solution

This limits the results to one particular user:

SELECT date, duration, educationDepartment, completedtasks 
FROM programm_completedtask 
WHERE date BETWEEN '2014-04-07' AND '2014-04-11'
AND trainee_id = (select id from users where username = 'XYZ');

OTHER TIPS

Something like this?

SELECT table2.username, table1.date, table1.duration, table1.educationDepartment, table1.completedtasks FROM table1 LEFT JOIN table2 ON table2.id = table1.trainee_id WHERE table1.date BETWEEN "2014-04-07" AND "2014-04-11" AND table1.username = 'someuser';

If you want to get the result from both tables, you should use INNER JOIN:

SELECT u.username, p.date, p.duration, p.educationDepartment, p.completedtasks 
FROM programm_completedtask AS p
INNER JOIN users AS u ON u.id = p.trainee_id
WHERE date BETWEEN "2014-04-07" AND "2014-04-11"
AND u.username = "knownusername";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top