|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Exclude options based on already chosen options
I have two tables
Code:
+-------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------+------+-----+---------+----------------+ | id | int(11) unsigned | NO | PRI | NULL | auto_increment | | description | varchar(255) | NO | | | | +-------------+------------------+------+-----+---------+----------------+ Code:
+-------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------+------+-----+---------+----------------+ | id | int(11) unsigned | NO | PRI | NULL | auto_increment | | description | varchar(255) | NO | | | | +-------------+------------------+------+-----+---------+----------------+ joined in a many to many relationship by table: Code:
+------------------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+------------------+------+-----+---------+-------+ | lane_id | int(11) unsigned | NO | PRI | | | | configuration_id | int(11) unsigned | NO | PRI | | | +------------------+------------------+------+-----+---------+-------+ what I need to do is exclude certain items from the configuration table based on other selected configurations. for example: the lanes table holds the following rows: Code:
+----+-------------+ | id | description | +----+-------------+ | 1 | Lane 1 | | 2 | Lane 2 | | 3 | Lane 3 | | 4 | Lane 4 | +----+-------------+ the configuration table contains: Code:
+----+------------------+ | id | description | +----+------------------+ | 1 | Lanes 2, 3 and 4 | | 2 | Lanes 3 and 4 | | 3 | Lane 1 | | 4 | Lane 2 | | 5 | Lane 3 | | 6 | Lane 4 | +----+------------------+ and the join table contains the appropriate links to create the described relationships. this is a reservation system, so if, for example, configuration 2 is reserved that would automatically exclude configurations 1, 2, 5 and 6. I've accomplished this with PHP code, but I feel it would be better and more efficient with SQL. 1. am i right? 2. i'm not an SQL expert so if someone could instruct me on how to do it that would be great. thanks, josh |
|
#2
|
||||
|
||||
|
there are two things i don't understand about your statement of the problem
1. "what I need to do is exclude certain items from the configuration table based on other selected configurations" what does "exclude from" mean? you want to DELETE rows in the config table? 2. "if, for example, configuration 2 is reserved that would automatically exclude configurations 1, 2, 5 and 6." assuming configuration 2 is linked via the many-to-many table, what does "exclude from" mean here? you want to SELECT lanes but not include configurations that are not linked? this confusion probably explains why no one has answered your question so far... |
|
#3
|
|||
|
|||
|
hmm... I didn't think of that... sorry.
All of the "exclusion" I am talking about is related to SELECT statements. So using the example below, if configuration 2 is reserved (which is handled by another table, but not relevant to the question) I want to be able to perform a select to retrieve open lanes. Since configuration 2 contains lanes 3 and 4, all other configurations that contain one or both of those should not be included in the results of the select. if that does not make sense let me know. Thanks, josh |
|
#4
|
||||
|
||||
|
Quote:
Code:
SELECT lanes.id
, lanes,description
FROM lanes
LEFT OUTER
JOIN reservations
ON reservations.lane_id = lanes.id
WHERE reservations.lane_id IS NULL
![]() |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Exclude options based on already chosen options |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|