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

+2


source to share


4 answers


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.

+2


source


Do something like this:



SELECT tbl1.col1 as tbl1_col1,tbl2.userid as tbl2_usrid, tbl2.col3 as tbl2_usrid_custcol
FROM tbl1,tbl2
WHERE tbl1.userid = tbl2.userid;

      

+1


source


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.

0


source


This should be done either as dynamic SQL (not ideal) or at the presentation layer (highly preferred).

0


source







All Articles