-1

我刚开始使用 MySQL,我在任何地方都找不到答案,可能是因为它不存在?

我想把这两个放在一张表中作为两列

SELECT CONCAT(Name, ' ',Category) as Pet_Name_and_Category
FROM Animal

SELECT CONCAT(firstname, ' ',lastname) as Customer_Name
From PetCustomer

所以我想要的表看起来像:

Gary, Snail               Spongebob Squarepants

Sandy, Squirrel           Patrick Star

这些是同一个表中的两个单独的列。

4

4 回答 4

0

尝试这个,

select Name || ' ' || Category from Animal
union
select firstname || ' ' || lastname from PetCustomer;

如果您希望这些作为单独的列,那么您可以使用它,

select Name || ' ' || Category as pet, firstname || ' ' || lastname as owner
from Animal, PetCustomer
where <join_condition_depending_on_table_structure>;
于 2013-02-06T08:03:26.683 回答
0

你需要使用一个INNER JOIN,所以像:

SELECT 
    CONCAT(Name, ' ',Category) as Pet_Name_and_Category, 
    CONCAT(firstname, ' ',lastname) as Customer_Name
FROM Animal 
    INNER JOIN PetCustomer ON Animal.Customer_id = PetCustomer.id

连接语法取决于 2 个表的关联方式。换句话说

你怎么知道“海绵宝宝”拥有“加里,蜗牛”?


如果它们不相关并且您只想将它​​们显示在彼此旁边,请使用 2 个单独的查询。(PHP 中的示例)

$animals = $db->query("SELECT CONCAT(Name, ' ',Category) as Pet_Name_and_Category FROM Animal");
$customers = $db->query("SELECT CONCAT(firstname, ' ',lastname) as Customer_Name From PetCustomer");

while (list($animal) = $animals->fetch_row() || list($customer) = $customers->fetch_row()) {
    echo $animal, "\t", $customer, "\n";
}
于 2013-02-06T08:06:42.757 回答
0

也许尝试子查询:

SELECT 
    (SELECT CONCAT(Name, ' ',Category) 
     FROM Animal 
     WHERE <column> = <search_argument>)  as Pet_Name_and_Category ,
    (SELECT CONCAT(firstname, ' ',lastname) 
     FROM PetCustomer 
     WHERE <column> = <search_argument>) AS Customer_Name
FROM <first table>
于 2013-02-06T08:11:19.207 回答
0

如果您需要对CONCAT同一张表使用两次,无论条件如何,都可以使用:

SELECT 
    CONCAT(TitleOfCourtesy, ' ', FirstName, ' ', LastName) AS NameEmployee, 
    CONCAT(PostalCode, ' ', Country, ' ', Region, ' ', City, ' ', [Address]) AS FullAddress
FROM Employees 
WHERE 1=1
于 2022-02-11T21:25:45.727 回答