简体   繁体   中英

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

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?

SELECT tget.* FROM contact AS tget ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM