Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Select all values from one table in a join while not knowing all column names?

Tags:

sql

mysql

Hello there I wonder if the following is possible (in MySQL): There is a simple query joining two tables:

SELECT * FROM `contact_attending_appointment` AS tcon,`contact` AS tget WHERE tcon.appointment_id = 2 AND tget.id = tcon.contact_id;

This query will return the values of both tables - tcon and tget - joined together. However, I want only to have the columns of ONE table.

Basically in SQL you achieve it like this:

SELECT col_1,col_2,...,col_n FROM ...

Or you get all the columns with

SELECT * FROM ...

However I would like to have something like (as I do not know the columns of tget by their names)

SELECT [* from tget] FROM ...

Is there a general solution for this or is this not possible?

like image 642
Blackbam Avatar asked Feb 23 '14 17:02

Blackbam


2 Answers

SELECT tget.* FROM contact AS tget ...
like image 85
Rafa Paez Avatar answered Oct 14 '22 07:10

Rafa Paez


The question has already been answered by Rafa. But you seem to be learning SQL. The following is a better way of writing your query:

SELECT c.*
FROM `contact_attending_appointment` caa INNER JOIN
     `contact` c
     ON c.id = caa.contact_id 
WHERE caa.appointment_id = 2;

The important changes are:

  1. Using proper ANSI join syntax, where the conditions are in the on clause rather than in the where.
  2. Using table aliases that are abbreviations for the table name. Someone looking at the query (even you in two months time) will find it easier to follow the logic.
like image 22
Gordon Linoff Avatar answered Oct 14 '22 09:10

Gordon Linoff