You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL查询:如何区分供应商/客户公司并按名称升序排列

Solution for Your Query

Got it, let's tackle this problem step by step. The key here is to union your Suppliers and Customers tables while explicitly adding a type column to distinguish between the two.

Assuming your tables are named Suppliers and Customers, and both have a CompanyName column for the company name (adjust these names if your schema is different), here's the complete query:

SELECT 
    CompanyName,
    'Supplier' AS CompanyType  -- This adds the type identifier for suppliers
FROM Suppliers
UNION ALL
SELECT 
    CompanyName,
    'Customer' AS CompanyType  -- This adds the type identifier for customers
FROM Customers
ORDER BY CompanyName ASC;

Breakdown of what this does:

  • UNION ALL combines the results from both tables without removing duplicates (use UNION instead if you want to eliminate any company names that exist in both tables, but UNION ALL is faster if duplicates aren't a concern).
  • The AS CompanyType clause creates a new column where we hardcode the type ('Supplier' or 'Customer') based on which table the row comes from.
  • ORDER BY CompanyName ASC sorts the combined results alphabetically by company name, just like you need.

If your table names or column names are different (e.g., SupplierName instead of CompanyName), just swap those out with your actual schema names and this will work perfectly.

内容的提问来源于stack exchange,提问作者Bontano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:39:05