简体   繁体   中英

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).

  • I would like to avoid using 'As alias_name' and use original column name
  • My tables have no common ID columns
  • Both my select statements returns 1 row each (1 row from Table A and 1 row from Table B)
  • 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?)

The following statement is close to what I require - It returns 2 columns (alias_name, alias_imageurl) from 2 tables:

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)

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';

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