简体   繁体   English

MySQL单行多列多表

[英]MySQL Single Row Multiple Columns Multiple Tables

I have two tables 'table A' and 'table B', I would like to select all columns from Table A (10 columns) and select 1 column from Table B so that I have 1 row with a total of 11 columns (10 from Table A and 1 from Table B). 我有两个表“表A”和“表B”,我想从表A中选择所有列(10列),然后从表B中选择1列,这样我就有1行,总共11列(表A和表B中的1)。

  • I would like to avoid using 'As alias_name' and use original column name 我想避免使用'As alias_name'并使用原始列名
  • My tables have no common ID columns 我的表没有通用的ID列
  • Both my select statements returns 1 row each (1 row from Table A and 1 row from Table B) 我的两个select语句均返回1行(表A中的1行和表B中的1行)
  • I just want to take my result from first select statement and join the result with second select statement to have multiple columns (1 row) and NOT a single column (so I will not be using UNION, perhaps INNER JOIN but not sure how to use it?) 我只想从第一个select语句中获取结果,然后将结果与第二个select语句合并,以具有多列(1行)而不是单个列(因此我将不会使用UNION,也许不是INNER JOIN,但不确定如何使用它?)

The following statement is close to what I require - It returns 2 columns (alias_name, alias_imageurl) from 2 tables: 以下语句与我的要求很接近-它从2个表中返回2列(alias_name,alias_imageurl):

SELECT (SELECT name FROM `users`) AS alias_name,(SELECT imageurl FROM `pictures` WHERE profilepicture LIKE '1') AS alias_imageurl

The problem with the above (besides being forced to use an alias for column names) is that I can only return 1 column from Table A instead of all because the below query returns an error: Operation should contain 1 column(s) 上面的问题(除了被迫为列名使用别名外)是我只能从表A中返回1列,而不是全部,因为下面的查询返回错误:操作应包含1列

SELECT (SELECT * FROM `users`),(SELECT imageurl FROM `pictures` WHERE profilepicture LIKE '1') AS alias_imageurl

Is this what you want? 这是你想要的吗?

SELECT u.*, p.imageurl
FROM users u cross join
     picture p
WHERE p.profilepicture LIKE '1';

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

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