I have a query:
Select A.col1 as col1, B.col5 as col2, C.col10 as col3
FROM Table A
JOIN Table B on(A.col2 = B.col2)
JOIN table C on(C.col1 = B.col3)
UNION
SELECT A.col1 as col1, B.col5 as col2, NULL as col3
FROM Table A
JOIN Table B on (A.col2 = B. col2)
where A.col4 != 'somevalue'
Any way of making this faster??
Table A
--------
col1 col2 col4
gerry 1 'somevalue'
harry 2 'othervalue'
tom 3
sarah 4 'somevalue'
col2 of table A is the primary key
Table B
-------
col2 col3 col5
1 45 34
1 34 23
1 56 67
2 34 56
Primary key of B is (col2, col3)
Table C
-------
col1 col10
34 'heyya'
467 'tyga'
56 'pity'
Primary key of C is also composite. one of these keys is col1
Output:
col1 col2 col3
gerry 23 'heyya'
gerry 67 'pity'
gerry 34 NULL
harry 56 'heyya'
So values of B that have presence in C or have 'somevalue' in A are called. Also values having both are also called.