[英]SQL to concatenate two columns and join two tables
Table person
:表
person
:
| id | f_name | l_name |
Table sales
:表
sales
:
| id | amount | date | itemname |
I have a problem joining the two tables which concat f_name
and last_name
as fullname column, and joining with table sales.我在加入将
f_name
和last_name
作为全名列的两个表并加入表 sales 时遇到问题。 Here id
is same on both tables.这两个表上的
id
相同。
Output: Output:
| itemname| date |fullname |
What I have tried:我试过的:
select *
from
(select
concat(f_name, l_name) as fullname
from
tblperson) p
left join
select itemname, date
from table sales s on s.id = p.id
It should actually be它实际上应该是
SELECT table_sales.*, concat(table_person.f_name, table_person.l_name) as fullname
FROM table_person
LEFT JOIN table_sales
ON table_person.id = table_sales.id
have not tested this but that is the syntax尚未对此进行测试,但这是语法
you're missing a field like person_id in your sales table (which references the id field in the person table).您在 sales 表中缺少像 person_id 这样的字段(它引用了 person 表中的 id 字段)。 You can then use a join to properly join the data together.
然后,您可以使用连接将数据正确连接在一起。
You are so close.你是如此接近。 Try this
尝试这个
select p.*,concat(f_name,l_name)as fullname, s.itemname,s.date, s.amount
from person p left join
sales s on s.id=p.id
Here ia good read on sql join.在这里,我很好地阅读了 sql 加入。 https://www.w3schools.com/sql/sql_join.asp
https://www.w3schools.com/sql/sql_join.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.