Display column alias from another tbl
I have 2 tables tbl1 and tbl2 tbl1 has userid col1 col2 col3 (multiple entries per user) EDIT tbl2 has userid col4 col5 col6 (single entry per user) In my original post I also had col3 which was random.
tbl2 is used to store custom column names for each user.
Now I need to know how I should display this column name in the results. those. how do I refer to the alias to pull this custom column name for each user.
SELECT col1 AS (Don't know what to put here ...)
Something like ... col1 AS tbl2.col3 where userid = "testuser" How can I do this? Maybe some kind of external / internal request ???
thank
source to share
The correct syntax is:
SELECT tbl2.userid, tb2.col3 AS [your name here], tbl1.col1 AS [your name here], ...
FROM tbl2
INNER JOIN tbl1 ON tbl1.userid = tb2.userid
WHERE tbl2.userid = "testuser"
The columns themselves are prefixed with the table name (not the alias). This is really necessary when two different tables share a column name, but it is not a bad habit to pick up even in simpler cases.
source to share
Present at the presentation level, not at the database side:
select
a.userid,
col1,
col2,
a.col3,
b.col3 as user_col1,
b.col4 as user_col2,
b.col5 as user_col3
from
tblA a
inner join tblB b on
a.userid = b.userid
where
userid='testuser'
Then from the application side, just use col4
, col5
and col6
as the column names as soon as you get to display the records.
source to share