tags:

views:

27

answers:

2

I have three following tables. The relationship is that each report_param and report_frequency is tied together using the intermediate table report_freq_map. Is it possible to construct an SQL so that user can select all the report_param rows using the constraint like frequency='daily', instead of using frequency='1'.

Thanks, -peter

mysql> describe report_params;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| test_level    | varchar(45)      | NO   |     | NULL    |                |
| sequence_name | varchar(45)      | NO   |     | NULL    |                |
| step_name     | varchar(45)      | NO   |     | NULL    |                |
| descriptions  | text             | NO   |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+
mysql> describe report_frequency;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| frequency | varchar(25)      | YES  |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+

mysql> describe report_freq_map;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| report_id | int(11) | NO   | PRI | 0       |       |
| freq_id   | int(11) | NO   | PRI | 0       |       |
+-----------+---------+------+-----+---------+-------+
+2  A: 

Try joining the tables using JOIN:

SELECT report_params.*
FROM report_params
JOIN report_freq_map ON report_id = report_params.id
JOIN report_frequency ON freq_id = report_frequency.id
WHERE frequency = 'daily'
Mark Byers
A: 
SELECT report_params.id, test_level, sequence_name, step_name, descriptions
FROM report_params
JOIN report_freq_map
ON report_params.id = report_id
JOIN report_frequency
ON freq_id = report_frequency.id
WHERE frequency = 'daily'

(add DISTINCT after SELECT if you want to allow more than one frequncy, or you might have duplicate entries)

Jasper