Mysql Select 1 column AS - mysql

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?

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.

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 "?

Related

How to force mysql to show schema name along with column names in a query?

I need to join 3 tables which have some columns with same name, like id and some foreign keys columns.
I make a select query and the results come with table names only. How to get results like "dbname"."columnname" in my queries so I can identify from which table is each columns without having to specify every columns in the query (using only an *)?
Note: I use Delphi with ZeosLib, so a solution using these tools would be OK as well. But I prefer to set this in the data base.
You have to create an alias for your field name in your query
SELECT a.ID, b.ID
FROM a
JOIN b
You need doblue quote " for field names with special characters, so change it to.
SELECT a.ID "a.ID", b.ID "b.ID"
OR
SELECT a.ID "MeaningfullName", b.ID "OtherName"
For example here I have two fields name "sent_dt" and change one to previous_time
SQL Fiddle Demo

MySQL - Duplicate columns after using Join operator

As stated in the title, I'm getting duplicate columns with this JOIN query.
A few tables are given and I want to write select statements to get only
the information from the tables which are needed.
Here is my SQL code so far:
SELECT mitarbeiter.PNR, pfleger.PNR, Name
from pfleger
JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR)
where Ort='Frankfurt';
After executing, I get the following result:
You can see the problem: I have two PNR columns which I don't want to have.
How can I remove the duplicate? I have tried SELECT DISTINCT ... but it doesn't accomplish my goal.
Just remove one from the select:
SELECT mitarbeiter.PNR, Name from pfleger JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR) where Ort='Frankfurt';
select distinct applies to rows, not columns. In the column list, just select one of the PNR columns:
SELECT mitarbeiter.PNR, Name from pfleger JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR) where Ort='Frankfurt';
In the select portion of the statement reference the PNR column from either of the tables (mitarbeiter, pfleger), but not both:
SELECT
mitarbeiter.PNR,
Name
from pfleger JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR)
where Ort='Frankfurt';
As other users have already mentioned, you just need to remove a field name from the SELECT clause. I just want to add that if the field you join on has the same name in both tables you can use special syntax, which allows to reference both columns as a single one:
SELECT PNR, Name
from pfleger
JOIN mitarbeiter USING (PNR)
where Ort='Frankfurt';

Select multiple columns from one alias

In my schema, there are two tables with the same column names (Changing them is not an option)
Performing a query with
select * from tabA join tabB results in a mysql error of 'Duplicate column name col'
The only solution to this is to select using aliases, however I do not want to type alias.col for every column (since I need all columns from both tables)
Is it possible to do something along the lines of:
select tabA.(colA, colB, colC...), tabB.(colA, colB, colC...)
no its not possible.
you have to do like that
select tabA.colA, tabA.colB, tabA.colC..., tabB.colA, tabB.colB, tabB.colC...
if you have same name column in both tables , just give it an other alias like that.
lets say you have id column name in both tables.
select tabA.id , tabB.id as id_B
will give you result
id id_B
Linger's fiddle

An efficient way to write MySql query that has duplicate column names

I have a reasonably straightforward MySQL query as follows:
SELECT *
FROM post INNER JOIN comment ON post.post_id = comment.post_id
WHERE post.host = 99999
ORDER BY post.post_id
My problem is that some of my column names are common to both the post and comment table. In the result, the column name appears only once, and its values are taken from the last table in the query (ie. the comment table).
I realise that I could explicitly list each column name in the SELECT part of the query, and use aliases to differentiate between duplicate column names, but there are a lot of columns in each table and it would be messy.
Is there a more efficient way to go about it?
You should just use aliases for those columns that are alike and then * for the rest:
SELECT post.post_id, comment.post_id as comment_post_id, *
FROM ...
I don't think there is a better approach.
Good luck.
Listing column names is always a better approach as long as it is feasible, it may give some hint to the MySQL optimizer.
Additionally, I would use Using syntax when the Join is done on similar column name.
SELECT x.x1, x.x2 ... , y.x1, y.x2 ...
FROM post as x
INNER JOIN comment as y
USING post_id
WHERE ...
You can select all the column from both the table.Just give an alias to the both table name and the use alias.* after select statement.
Then it will select all the column from the table.
Example:
SELECT pst.*,cmt.* from post pst INNER JOIN comment cmt
FROM ...

Efficient way to do unions and intersections in mySQL

I have a mySQL table with columns: name and label. If a person, "Bob" has the labels "cool","funny", and "childish", my table would have the corresponding rows: (Bob, cool), (Bob, funny), and (Bob, childish).
Is there an efficient way to select people based on labels with a boolean query? For example, in pseudo-SQL: SELECT name WHERE person IS (COOL OR NOT FUNNY) AND NOT CHILDISH.
I think I could hack something together using UNION, JOIN, maybe some sub-queries, but I was wondering if there was an efficient way to do this.
EDIT:
As of now, I am planning to distribute AND, ie ((COOL OR NOT FUNNY) AND NOT CHILDISH) => (COOL AND NOT CHILDISH) OR (NOT FUNNY AND NOT CHILDISH). And then I can determine each of the parts that are OR'd together with something like:
SELECT DISTINCT a.name
FROM `tags` AS a
JOIN `tags` AS b ON (a.label='cool' AND a.name=b.name AND b.name NOT IN (
SELECT name FROM `tags` WHERE label='funny'))
JOIN `tags` AS c ON (a.name=c.name AND c.name='childish')
# for "COOL AND NOT FUNNY AND CHILDISH"
And then use UNION to join them together.
For the negative checks, the most efficient way would be to use MINUS as follows:
SELECT NAME
FROM NAME_LABEL
WHERE LABEL IN ('COOL') -- use IN for easy matching of multiple labels
UNION
SELECT NAME
FROM NAME_LABEL NL
WHERE NOT EXISTS (SELECT * FROM NAME_LABEL WHERE NAME = NL.NAME AND LABEL IN ('FUNNY'))
MINUS
SELECT NAME
FROM NAME_LABEL
WHERE LABEL IN ('CHILDISH');
The MINUS keyword selects distinct rows from the first query and don't appear in the second query.
Performance would be better with an index on LABEL:
CREATE INDEX NAME_LABEL_NAME ON NAME_LABEL(NAME);
Unfortunately, the "NOT FUNNY" requires an EXISTS subquery. If you use a join, the MySQL query optimizer turns it into a subselect anyway :(