Hi,
I want to get some results via query simillar to:
SELECT * FROM users LEFT JOIN IF (users.type = '1', 'private','company') AS details ON users.id = details.user_id WHERE users.id = 1
Any ideas?
Hi,
I want to get some results via query simillar to:
SELECT * FROM users LEFT JOIN IF (users.type = '1', 'private','company') AS details ON users.id = details.user_id WHERE users.id = 1
Any ideas?
SELECT
users.*,
details.info,
CASE users.type WHEN '1' THEN 'private' ELSE 'company' END AS user_type
FROM
users
INNER JOIN (
SELECT user_id, info FROM private
UNION
SELECT user_id, info FROM company
) AS details ON details.user_id = users.id
EDIT: Original version of the answer (question misunderstood):
SELECT
*,
CASE type WHEN '1' THEN 'private' ELSE 'company' END AS details
FROM
users
WHERE
users.id = 1
SELECT * FROM users
LEFT JOIN private AS details ON users.id = details.user_id
WHERE users.id = 1 AND users.type = 1
UNION
SELECT * FROM users
LEFT JOIN company AS details ON users.id = details.user_id
WHERE users.id = 1 AND users.type != 1
I think that is what you are trying to do, isn't it?
As you have now said that the number of columns differs, you would need to specify the columns, e.g.
SELECT 'private' AS detailType, users.*, col1, col2, col3, '' FROM users
LEFT JOIN private AS details ON users.id = details.user_id
WHERE users.id = 1 AND users.type = 1
UNION
SELECT 'company', users.*, col1, '', '', col4 FROM users
LEFT JOIN company AS details ON users.id = details.user_id
WHERE users.id = 1 AND users.type != 1
In this example, private has columns col1, col2 and col3, whilst company has col1 and col4, but you want them all.