ripcurlksm
09-15-2008, 04:49 PM
I have two permissions tables. "user_permissions_single" contains company_ids, if a member is only subscribed to a select few companies.
user_permissions_single
===========================
user_id | company_id
------------------------
1 | 10
1 | 11
2 | 12
Another table contains market_ids, if a member is subscribed to a certain market. (This prevents from writing multiple company_ids rows, when it can be handled with just one row.)
user_permissions_market
===========================
user_id | market_id
------------------------
2 | 1
2 | 2
market
===========================
market_id | name
------------------------
1 | red
2 | green
So its easy to query the user_permissions_single table.
SELECT
c.name
, c.description
FROM user as u
INNER
JOIN user_permissions_single as p
ON p.user_id = u.user_id
INNER
JOIN company as c
ON c.company_id = p.company_id WHERE u.user_id='$user_id'
Its also easy to query the user_permissions_market table
SELECT
c.name,
c.description
FROM user as u
INNER
JOIN user_permissions_market as p
ON p.user_id = u.user_id
INNER
JOIN market as e
ON e.market_id = p.market_id
INNER
JOIN company as c
ON c.company_id = e.company_id
WHERE u.user_id='$user_id'
But what if a user is subscribed to several categories AND a few "one-off" company's? How can I get the company_id's from both these table joins into one long list? I tried this subquery below, but MySQL says subquery returns more than one row. How can I allow for multiple rows? Is subquery the wrong method?
This is returning a 'more than one row' error. (NOTE: I modified the select statements for both joins to output company_ids instead, since we are trying to match them)
SELECT * FROM company WHERE company_id = (SELECT
c.company_id
FROM user as u
INNER
JOIN user_permissions_single as p
ON p.user_id = u.user_id
INNER
JOIN company as c
ON c.company_id = p.company_id WHERE u.user_id='$user_id') OR (SELECT
c.company_id
FROM user as u
INNER
JOIN user_permissions_market as p
ON p.user_id = u.user_id
INNER
JOIN market as e
ON e.market_id = p.market_id
INNER
JOIN company as c
ON c.company_id = e.company_id
WHERE u.user_id='$user_id')
How can output the company ids from both table joins into one list?
user_permissions_single
===========================
user_id | company_id
------------------------
1 | 10
1 | 11
2 | 12
Another table contains market_ids, if a member is subscribed to a certain market. (This prevents from writing multiple company_ids rows, when it can be handled with just one row.)
user_permissions_market
===========================
user_id | market_id
------------------------
2 | 1
2 | 2
market
===========================
market_id | name
------------------------
1 | red
2 | green
So its easy to query the user_permissions_single table.
SELECT
c.name
, c.description
FROM user as u
INNER
JOIN user_permissions_single as p
ON p.user_id = u.user_id
INNER
JOIN company as c
ON c.company_id = p.company_id WHERE u.user_id='$user_id'
Its also easy to query the user_permissions_market table
SELECT
c.name,
c.description
FROM user as u
INNER
JOIN user_permissions_market as p
ON p.user_id = u.user_id
INNER
JOIN market as e
ON e.market_id = p.market_id
INNER
JOIN company as c
ON c.company_id = e.company_id
WHERE u.user_id='$user_id'
But what if a user is subscribed to several categories AND a few "one-off" company's? How can I get the company_id's from both these table joins into one long list? I tried this subquery below, but MySQL says subquery returns more than one row. How can I allow for multiple rows? Is subquery the wrong method?
This is returning a 'more than one row' error. (NOTE: I modified the select statements for both joins to output company_ids instead, since we are trying to match them)
SELECT * FROM company WHERE company_id = (SELECT
c.company_id
FROM user as u
INNER
JOIN user_permissions_single as p
ON p.user_id = u.user_id
INNER
JOIN company as c
ON c.company_id = p.company_id WHERE u.user_id='$user_id') OR (SELECT
c.company_id
FROM user as u
INNER
JOIN user_permissions_market as p
ON p.user_id = u.user_id
INNER
JOIN market as e
ON e.market_id = p.market_id
INNER
JOIN company as c
ON c.company_id = e.company_id
WHERE u.user_id='$user_id')
How can output the company ids from both table joins into one list?