简体   繁体   English

SQL:在不知道所有列名的情况下,从连接中的一个表中选择所有值?

[英]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: 您好,我想知道以下是否可行(在MySQL中):有一个简单的查询连接两个表:

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. 此查询将返回两个表(tcon和tget)连接在一起的值。 However, I want only to have the columns of ONE table. 但是,我只希望拥有ONE表的列。

Basically in SQL you achieve it like this: 基本上在SQL中你可以像这样实现它:

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) 但是我希望有类似的东西(因为我不知道他们名字的tget列)

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. 但你似乎在学习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 . 使用正确的ANSI连接语法,其中条件在on子句而不是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. 有人查看查询(即使你在两个月的时间内)会发现更容易遵循逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不知道列名的情况下将值从一个表复制到另一个表 - Copying values from one table to another without knowing column names 从表A中选择所有内容,从表B中选择一列-LEFT JOIN - Select all from Table A, one column from Table B - LEFT JOIN 更好的方法来选择第一个表中的所有列,并在内连接上从第二个表中选择一列 - Better way to select all columns from first table and only one column from second table on inner join 在连接中,如何在所有列名前加上它来自的表 - In a join, how to prefix all column names with the table it came from 从具有特定条目的表中选择所有 COLUMN_NAMES - SELECT all COLUMN_NAMES from a table that have a specific entry MySQL查询 - 连接表以显示一个表中的所有值,并仅匹配其他列中的结果 - MySQL Query - join tables to show all values from one table and only matching results in other column 从 laravel 中的一个特定表中获取所有索引列名称 - get all indexed column names from one specific table in laravel 如何从表中选择具有相同但随机值的列中的行,而只通过一个查询却不知道该值? - How to select rows from a table with identical but random values in a column without knowing that value with only one query? MySQL从一个表的列中选择一些值(不是所有值),并根据所选值在其他表中求和值 - MySQL select some values (not all values) from column of one table and depending on the selected values sum values in other table 如何将一列连接到另一个表中的所有列 - how to join one column to all columns from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM