puks1978
puks1978

Reputation: 3697

Mysql Select 1 column AS

I am wanting to select 1 column from my select statement as but then leave the rest as is so:

SELECT tbl_user.reference AS "reference", * FROM tbl_user JOIN tbl_details ON.....

Is this possible?

Upvotes: 0

Views: 11611

Answers (2)

p.campbell
p.campbell

Reputation: 100567

Yes. You can use double quotes like that to create a column alias. You can SELECT a column twice (or more) in your SELECT list.

Try something like this, where you can give each "reference" column its own alias:

 SELECT u.reference AS UserReference,
        d.reference as DetailsReference,
        u.id, /*etc etc*/
 FROM   tbl_user AS U
 JOIN   tblDetails AS D ON ....           

You mention in the comments that you want all columns from each table, while being able to distinguish between the reference columns(likely named the same in both tables). Suggest NOT using SELECT *, as it's an anti-pattern. It's most beneficial to specify your column list in your SELECT statement, and do your query engine a favour of not having to look up the list of columns on each table.

Upvotes: 1

Sinthia V
Sinthia V

Reputation: 2093

If you just want one column, this will work:

SELECT SELECT tbl_user.username AS "username" FROM tbl_user JOIN tbl_details on tbl_user.key LIKE tbl_details.key

What do you mean by "but then leave the rest as is "?

Upvotes: 0

Related Questions