mysql multi group query with count - php

I have this query
SELECT
products_list.id_cat_unique,
products_categories_list.*,
COUNT(products_list.id_cat_unique) as counter
FROM products_categories_ids
LEFT JOIN products_list
ON products_categories_ids.id_cat_unique = products_list.id_cat_unique
LEFT JOIN products_categories_list
ON products_categories_list.id_cat_unique = products_categories_ids.id_cat_unique
GROUP BY products_categories_list.name_cat
ORDER BY products_categories_list.name_cat ASC
This is complicated to explain without having the tables on your screen but I'll try
what I want is count all the rows from products_list that can be associated with max two rows per id_cat_unique (contained in products_categories_list) that are English or Italian. This two items each could have infinite id_products in products_list of course.
I want the result to be grouped by products_categories_list.name_cat and by the language.
What I get with this query is categories grouped by name/unique id which is correct, the problem is that the counter value for both English and Italian turns out as the sum of all the products associated with one id_cat_unique regardless of the language. So if I have 12 rows from one category in English and 3 for the same category in Spanish I get 15 as counter for both the languages.
EDIT ADDING TABLES STRUCTURES
products_list
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| id_product | int(11) | NO | PRI | NULL | auto_increment |
| id_product_unique | int(5) | NO | | NULL | |
| lang | varchar(2) | NO | | NULL | |
| name_product | varchar(200) | NO | | NULL | |
| desc_product | text | NO | | NULL | |
| id_cat_unique | int(2) | NO | | NULL | |
| status | int(1) | NO | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)
products_categories_ids
+---------------+--------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------+------+-----+---------+----------------+
| id_cat_unique | int(5) | NO | PRI | NULL | auto_increment |
+---------------+--------+------+-----+---------+----------------+
1 row in set (0.00 sec)
products_categories_list
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id_cat | int(5) | NO | PRI | NULL | auto_increment |
| id_cat_unique | int(2) | NO | | NULL | |
| lang | varchar(2) | NO | | NULL | |
| name_cat | varchar(500) | NO | | NULL | |
| date_created | int(11) | NO | | NULL | |
| date_modified | int(11) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
result from query
| id_cat_unique | id_cat | id_cat_unique | lang | name_cat | date_created | date_modified | counter |
+---------------+--------+---------------+------+--------------------------------+--------------+---------------+---------+
| 1 | 18 | 1 | it | Carne di suino | 1308267538 | 1308267538 | 6 |
| 14 | 21 | 14 | it | Guanciali | 1308777322 | 1308777322 | 2 |
| 3 | 20 | 3 | it | Pollo a pezzi | 1308267892 | 1308267892 | 2 |
| 1 | 22 | 1 | en | Pork meat | 1308267538 | 1312383232 | 6 |
| 14 | 23 | 14 | en | Sheeps | 1308777322 | 1312383220 | 2 |
| 2 | 19 | 2 | it | That's a "test" | 1308267538 | 1308267538 | 7 |
+---------------+--------+---------------+------+--------------------------------+--------------+---------------+---------+
6 rows in set (0.00 sec)
"Pork meat" and "Carne di Suino" have respectively 1 and 5 products (from the same category but different language, i wanna group the counting by language too) instead it shows 6 for both
NEW question
I'd like to use this query for many purposes without using different things, so accordingly if I want to retrieve the categories with ONE language I'd add one where clause
select pcl.*,
(select count(*) from products_list pl
where pcl.id_cat_unique = pl.id_cat_unique
and pcl.lang = pl.lang) as counter
from products_categories_list pcl
where products_list.lang = 'en' <- added
ORDER BY pcl.name_cat ASC
I get Unknown column 'products_list.lang' in 'where clause'. Why?

I THINK this will work, if not, it's going to be close, let me know what you get, and I can modify it. I've modified the query around. Would this work?
select pcl.*,
(select count(*) from products_list pl
where pcl.id_cat_unique = pl.id_cat_unique
and pcl.lang = pl.lang) as counter
from products_categories_list pcl
ORDER BY pcl.name_cat ASC
The PCL and PL are just aliases for the tables, so I didn't have to always write out products_categories_list and products_list. You specify the alias in the from part of the statement, so from products_list pl allows you to refer to products_list as pl in the rest of the query.
The idea behind this is that you have a unique row for each products_categories_list and you want a count of a secondary table. This could have been done by group by or by a sub query. I put the correlated subquery in to count the number of rows in products_list that matched the products_categories_list and language, since you are getting all the columns out of the products_categories_list. The correlated subquery allows you to get a single value (count(*)) from a nested query.

Probably this: add DISTINCT
COUNT(DISTINCT products_list.id_cat_unique) as counter
This will ignore duplicate, so the 12+3 via the JOIN becomes 1+1

Related

How do I merge all product tables while adding new column?

Summary
I have many tables that hold product information. The tables are all named like this:
p_productCategory
Here is some real data (showing only three for space)
+---------------------+
| Tables_in_global |
+---------------------+
| orders |
| p_audio_buzzers |
| p_audioaccessories |
| p_batteries_primary |
+---------------------+
The Task At Hand
I need to consolidate all these tables into ONE table. They all have the same column names:
+--------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+--------------+------+-----+---------+-------+
| Datasheets | varchar(128) | YES | | NULL | |
| Image | varchar(85) | YES | | NULL | |
| DigiKey_Part_Number | varchar(46) | YES | | NULL | |
| Manufacturer_Part_Number | varchar(47) | YES | | NULL | |
| Manufacturer | varchar(49) | YES | | NULL | |
| Description | varchar(34) | YES | | NULL | |
| Quantity_Available | int(11) | YES | | NULL | |
| Factory_Stock | bit(1) | YES | | NULL | |
| Unit_Price | decimal(9,5) | YES | | NULL | |
| qty | bit(1) | YES | | NULL | |
| Minimum_Quantity | int(11) | YES | | NULL | |
| Packaging | varchar(4) | YES | | NULL | |
+--------------------------+--------------+------+-----+---------+-------+
Also, I need to add a new column "category", which would be the table name, ie: audioaccessories, batteries_primary
Question:
How would I go about making a new "products" table, and inserting all the records from all the tables prefaced with a p_ ?
Thank you for looking. I am stuck. I looked at some other "merge table" questions on here, but none were similar enough for me to use.
Write a query by using UNION between all the tables. Make sure to statically add "category" for each projection. Attach the query block right after CREATE TABLE segment.
Example:CREATE TABLE new_table_name AS SELECT *,'audio_buzzers' AS category FROM p_audio_buzzers UNION SELECT *,'audioaccessories' AS category FROM p_audioaccessories UNION SELECT *,'batteries_primary' AS category FROM p_batteries_primary

Need to speed up MySQL query, it's at 3+ seconds with only 4,000 records, has 3 joins

Here's the situation:
I have a few tables (described below) that track residents in an apartment building, their unit number, and when they were "seen" last (we have lots of older people with health issues, so it's important to check on them every 2 days or so; sometimes they die here and that's how we know how to check on them).
The contstraints for a "check" are that they have to have been seen in the past 48 hours; if not, the query should pull their record up. Here are the table definitions I'm using:
The "people" table, where resident info is stored:
MariaDB [olin2]> describe people;
+-------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fname | varchar(32) | NO | MUL | NULL | |
| lname | varchar(32) | NO | | NULL | |
| dob | date | YES | | NULL | |
| license_no | varchar(24) | NO | | NULL | |
| date_added | timestamp | NO | | CURRENT_TIMESTAMP | |
| status | varchar(8) | NO | | Allow | |
| license_exp | date | YES | | NULL | |
+-------------+-------------+------+-----+-------------------+----------------+
The "units" table, where unit numbers are stored (people switch units so I didn't want them in the "people" table):
MariaDB [olin2]> describe units;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| number | varchar(3) | NO | MUL | NULL | |
| resident | int(11) | NO | | NULL | |
| type | varchar(16) | NO | | NULL | |
+----------+-------------+------+-----+---------+----------------+
and the "wellness" table, where the "checks" are stored (the resident's id number, when they were seen and by whom, etc.):
MariaDB [olin2]> describe wellness;
+--------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+-------------------+----------------+
| wellness_id | int(11) | NO | PRI | NULL | auto_increment |
| people_id | int(11) | NO | | NULL | |
| time_checked | timestamp | NO | | CURRENT_TIMESTAMP | |
| check_type | varchar(1) | NO | | NULL | |
| username | varchar(16) | NO | | NULL | |
| return_date | timestamp | YES | | NULL | |
+--------------+-------------+------+-----+-------------------+----------------+
The "return_date" field in the "wellness" table is for when a resident leaves for more than 2 days, then they won't be included in the results when they are displayed (they actually will be included in the query results, but I use PHP to filter those out).
Here's the query I have been using... It worked well for a few weeks, but as there were more and more records added it's been getting noticably slower (right now its 3.5 seconds to return the results):
select p.id, w.time_checked, w.username, w.return_date
from people p
left join units u on p.id = u.resident
left join wellness w on p.id = w.people_id
left join wellness as w2 on w.people_id = w2.people_id
and w.time_checked < w2.time_checked
where w2.people_id is null
and w.time_checked < (now() - interval 48 hour)
order by u.number
I know my problem is the joins, but I don't know how to get the results I need without them and/or how to optimize this query to speed it up... Here's a sample of results (if needed):
+----+---------------------+----------+---------------------+
| id | time_checked | username | return_date |
+----+---------------------+----------+---------------------+
| 8 | 2013-12-01 11:00:13 | tluce | 0000-00-00 00:00:00 |
+----+---------------------+----------+---------------------+
1 row in set (3.44 sec)
So, in this result set, resident 8 hasn't been seen for 3 days... the result is correct but the 3.44 sec isn't acceptable for my users to have to wait.
Any ideas on how I can improve this?
EDIT (More Info):
I realize updating the wellness entry for each person would be easier and quicker to access; however I like to have these data on-hand because I generate graphs from it to show A) when we most often see a particular resident and B) which staff members check on people the most often (aka - who's doing their job and who's not)
I DO use indexes, and here's the results of an EXPLAIN on my query:
+------+-------------+-------+--------+---------------+---------+---------+----------------- -+------+--------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+--------+---------------+---------+---------+----------------- -+------+--------------------------------------------------------------------+
| 1 | SIMPLE | u | ALL | NULL | NULL | NULL | NULL | 107 | Using temporary; Using filesort |
| 1 | SIMPLE | p | eq_ref | PRIMARY,idx | PRIMARY | 4 | olin2.u.resident | 1 | Using where |
| 1 | SIMPLE | w | ALL | NULL | NULL | NULL | NULL | 7074 | Using where; Using join buffer (flat, BNL join) |
| 1 | SIMPLE | w2 | ALL | NULL | NULL | NULL | NULL | 7074 | Using where; Not exists; Using join buffer (incremental, BNL join) |
+------+-------------+-------+--------+---------------+---------+---------+----------------- -+------+--------------------------------------------------------------------+
The indexes in the people table: id, fname, lname, license_no
The wellness table: wellness_id
The units table: id, number
Under possible keys each field that has NULL means that index was not used. so you can add index to the fields you use to join. for example units.residents. you can do the same thing to wellness.people_id –
You have too many joins for this result. The only reason it seems like you have the "people" table in there is to attach to "units" but you have flags in both "units" and "wellness" that you can join off of. I also don't believe the second join of the wellness table is necessary.
This is all you should need:
Select W.people_id, w.time_checked, w.username, w.return_date
FROM units u
left join wellness w on u.resident = w.people_id
where w.time_checked < (now() - interval 48 hour)
order by u.number
You may want to also look at indexing your people_id fields in tables where it is not the primary key. That will help speed up any queries you try and run off these tables.

strange behavior of query

I have the table below:
+-------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+-------------------+----------------+
| category_id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(64) | NO | | NULL | |
| description | text | NO | | NULL | |
| created_on | timestamp | NO | | CURRENT_TIMESTAMP | |
| updated_on | timestamp | YES | | NULL | |
+-------------+------------------+------+-----+-------------------+----------------+
I needed to delete some records according to the date created and executed a set of queries:
mysql> select created_on from categories limit 10;
+---------------------+
| created_on |
+---------------------+
| 2013-01-14 09:26:21 |
| 2012-08-15 11:18:38 |
| 2012-09-06 06:44:46 |
| 2012-09-06 23:27:14 |
then
mysql> select date('2013-01-14 09:26:21');
+-----------------------------+
| date('2013-01-14 09:26:21') |
+-----------------------------+
| 2013-01-14 |
+-----------------------------+
and finally I got:
mysql> select DATE('created_on') from categories limit 10;
+--------------------+
| DATE('created_on') |
+--------------------+
| NULL |
| NULL |
| NULL |
| NULL |
As you can see the date wasn't converted correctly. In about 10 minutes is worked ok:
mysql> select date(created_on) from categories limit 10;
+------------------+
| date(created_on) |
+------------------+
| 2013-01-14 |
| 2012-08-15 |
| 2012-09-06 |
| 2012-09-06 |
I'm sure that the data were intact while I was running the queries.
So my question:
Can anybody explain why date function showed different results on the same input?
You have to use::
select DATE(created_on) from categories limit 10;
By using the inverted comma, you are asking the database to treat 'created_on' as parameter, and since it cannot be converted to date type, you are getting null
select DATE('created_on') from categories limit 10;
String versus column:
select DATE(created_on) from categories limit 10;

tag cloud with mysql

Is there any other way to make tag cloud from mysql without temporary tables?
mysql> explain wallpaper_keywords;
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | mediumint(7) unsigned | NO | PRI | NULL | auto_increment |
| wallpaper_id | mediumint(7) unsigned | YES | MUL | NULL | |
| keyword_id | smallint(5) unsigned | YES | MUL | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+
mysql> explain keywords;
+---------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-----------------------+------+-----+---------+----------------+
| id | mediumint(7) unsigned | NO | PRI | NULL | auto_increment |
| keyword | varchar(32) | YES | UNI | NULL | |
+---------+-----------------------+------+-----+---------+----------------+
SELECT k.keyword,count(k.keyword) AS count FROM keywords k Left Join wallpaper_keywords wk ON wk.keyword_id = k.id GROUP BY wk.keyword_id, k.keyword ORDER BY count DESC limit 100;
+----------+-------+
| keyword | count |
+----------+-------+
| computer | 10 |
| road | 4 |
| tree | 4 |
| window | 4 |
| nature | 4 |
| forest | 3 |
| cars | 3 |
| mazda | 3 |
| abstract | 3 |
| funny | 3 |
| sport | 3 |
| sea | 3 |
| city | 3 |
| warrior | 2 |
| bird | 2 |
| lights | 1 |
| summer | 1 |
| space | 1 |
| dusk | 1 |
| ubuntu | 1 |
+----------+-------+
explain:
+----+-------------+--------------------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| 1 | PRIMARY | keywords | index | NULL | keyword | 99 | NULL | 20 | Using index; Using temporary; Using filesort |
| 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 54 | |
| 2 | DERIVED | wallpaper_keywords | index | NULL | PRIMARY | 3 | NULL | 54 | |
+----+-------------+--------------------+-------+---------------+---------+---------+------+------+----------------------------------------------+
3 rows in set (0.00 sec)
EDIT
another query:
SELECT count,k.keyword from (SELECT * , count(wk.keyword_id) AS count FROM wallpaper_keywords wk GROUP BY wk.keyword_id LIMIT 50000) as data left join keywords as k on k.id = data.keyword_id order by count desc limit 500
+----+-------------+------------+--------+---------------+------------+---------+-----------------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+------------+---------+-----------------+------+----------------+
| 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 20 | Using filesort |
| 1 | PRIMARY | k | eq_ref | PRIMARY | PRIMARY | 3 | data.keyword_id | 1 | |
| 2 | DERIVED | wk | index | NULL | keyword_id | 3 | NULL | 59 | |
+----+-------------+------------+--------+---------------+------------+---------+-----------------+------+----------------+
the question would be now - is there any better solution? cheaper (SHOW STATUS LIKE 'last_query_cost';) way to count it?
For counting total value for every keyword you cau use additional field (total_count) in table keywords and by every adding new keyword add +1 to this field - it's really simpler for your database in future, when you will have 100 000 records.

SQL Query using MySQL sample employee database

I have a problem making an advanced (for me) query.
I am trying to get the first name, last name, title, department name, and first and last name of the manager. I am using this SQL in PHP if for some reason you needed to know. I limit to 10 because the database is enormous. I need only the entry for the employees latest data. They have more than one entry because of job promotions, etc.
I included everything I think you would need to understand my problem. Thanks if you can help.
mysql>
SELECT DISTINCT employees.first_name, employees.last_name,
titles.title, departments.dept_name, z.first, z.last
FROM employees, dept_emp, departments, titles,
(
SELECT employees.first_name AS first, employees.last_name AS last
FROM employees, dept_emp, dept_manager
WHERE
employees.emp_no = dept_emp.emp_no
AND dept_manager.emp_no = dept_emp.emp_no
) AS z
WHERE
employees.emp_no = dept_emp.emp_no
AND dept_emp.dept_no = departments.dept_no
AND titles.emp_no = employees.emp_no
LIMIT 10;
+------------+-----------+-----------------+-------------+-----------+--------------+
| first_name | last_name | title | dept_name | first | last |
+------------+-----------+-----------------+-------------+-----------+--------------+
| Georgi | Facello | Senior Engineer | Development | Margareta | Markovitch |
| Georgi | Facello | Senior Engineer | Development | Vishwani | Minakawa |
| Georgi | Facello | Senior Engineer | Development | Ebru | Alpin |
| Georgi | Facello | Senior Engineer | Development | Isamu | Legleitner |
| Georgi | Facello | Senior Engineer | Development | Shirish | Ossenbruggen |
| Georgi | Facello | Senior Engineer | Development | Karsten | Sigstam |
| Georgi | Facello | Senior Engineer | Development | Krassimir | Wegerle |
| Georgi | Facello | Senior Engineer | Development | Rosine | Cools |
| Georgi | Facello | Senior Engineer | Development | Shem | Kieras |
| Georgi | Facello | Senior Engineer | Development | Oscar | Ghazalie |
+------------+-----------+-----------------+-------------+-----------+--------------+
10 rows in set (0.00 sec)
The database is the employees database from mysql.com:
http://dev.mysql.com/doc/index-other.html
mysql> desc departments;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dept_no | char(4) | NO | PRI | NULL | |
| dept_name | varchar(40) | NO | UNI | NULL | |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.22 sec)
mysql> desc dept_emp;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| emp_no | int(11) | NO | PRI | NULL | |
| dept_no | char(4) | NO | PRI | NULL | |
| from_date | date | NO | | NULL | |
| to_date | date | NO | | NULL | |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.20 sec)
mysql> desc dept_manager;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| dept_no | char(4) | NO | PRI | NULL | |
| emp_no | int(11) | NO | PRI | NULL | |
| from_date | date | NO | | NULL | |
| to_date | date | NO | | NULL | |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.21 sec)
mysql> desc employees;
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no | int(11) | NO | PRI | NULL | |
| birth_date | date | NO | | NULL | |
| first_name | varchar(14) | NO | | NULL | |
| last_name | varchar(16) | NO | | NULL | |
| gender | enum('M','F') | NO | | NULL | |
| hire_date | date | NO | | NULL | |
+------------+---------------+------+-----+---------+-------+
6 rows in set (0.32 sec)
mysql> desc salaries;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| emp_no | int(11) | NO | PRI | NULL | |
| salary | int(11) | NO | | NULL | |
| from_date | date | NO | PRI | NULL | |
| to_date | date | NO | | NULL | |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.34 sec)
mysql> desc titles;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| emp_no | int(11) | NO | PRI | NULL | |
| title | varchar(50) | NO | PRI | NULL | |
| from_date | date | NO | PRI | NULL | |
| to_date | date | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.60 sec)
I am trying to get the first name, last name, title, department name, and first and last name of the manager
Using the theory (but it's probably not optimized if you have large tables), do it step by step. Assuming you have a $date PHP variable that contains a date (like the result from date('Y-m-d 00:00:00')), or otherwise you may use MySQL's date.
1/ get employees' identity:
SELECT
e.first_name,
e.last_name
FROM
employees AS e
WHERE
1
2/ add his/her current title
SELECT
e.first_name,
e.last_name,
t.title
FROM
employees AS e,
titles AS t
WHERE
e.emp_no=t.emp_no AND t.from_date<='$date' AND t.to_date>='$date'
3/ get his/her current department (from dept_emp) and retrieve the department's name
SELECT
e.first_name,
e.last_name, t.title,
d.dept_name
FROM
employees AS e,
titles AS t,
dept_emp AS de,
departments as d
WHERE
e.emp_no=t.emp_no AND t.from_date<='$date' AND t.to_date>='$date'
AND
e.emp_no=de.emp_no AND de.from_date<='$date' AND de.to_date>='$date'
AND
d.dept_no=de.dept_no
4/ get the department's manager (from dept_manager) and retrieve his/her name (from employees)
SELECT
e.first_name AS empFN,
e.last_name AS empLN,
t.title AS empT,
d.dept_name AS dept,
em.first_name AS manFN,
em.last_name AS manLN
FROM
employees AS e,
titles AS t,
dept_emp AS de,
departments as d,
dept_manager AS dm,
employees AS em
WHERE
e.emp_no=t.emp_no AND t.from_date<='$date' AND t.to_date>='$date'
AND
e.emp_no=de.emp_no AND de.from_date<='$date' AND de.to_date>='$date'
AND
d.dept_no=de.dept_no
AND
em.emp_no=dm.emp_no AND dm.from_date<='$date' AND dm.to_date>='$date'
AND
de.dept_no=dm.dept_no
There you can append a LIMIT (and an ORDER BY probably, you don't know which results will be picked in case of a limit), and/or add in the WHERE an emp_no for the employee(s) you want to have info about. Not tested though, but it's mainly to let you understand how to build the request.
This is theory, and this request is probably be as heavy to process as the one you wrote. Since you use SQL with PHP, it may be better to retrieve only the primary keys you're interested in, then send short requests to retrieve the information you want. Something like that I mean (using fake functions but the spirit's here):
$dateString="from_date<='$date' AND to_date>='$date'";
$qe=query("SELECT emp_no, first_name, last_name, dept_no FROM employees, dept_emp WHERE dept_emp.emp_no=employees.emp_no LIMIT 10 ORDER BY emp_no DESC");
while($r=fetch($qe)) {
$qt=fetch(query("SELECT title FROM titles WHERE emp_no=".$qe['emp_no']." AND ".$dateString));
$qd=fetch(query("SELECT dept_name FROM departments WHERE dept_no=".$qe['dept_no']." AND ".$dateString));
$qm=fetch(query("SELECT first_name, last_name FROM employees AS e, dept_manager AS dm WHERE dept_no=".$qe['dept_no']." AND e.emp_no=dm.emp_no AND ".$dateString));
//echo/process here
}
Hope this helps :)

Categories