简体   繁体   English

提取匹配SQL字符串部分的SQL查询

[英]SQL query to extract the part of matching SQL string

Let's consider a CLASS and BILL table.让我们考虑一个CLASSBILL表。

CLASS table: CLASS表:

Class班级 Description描述
F F Factory工厂
H H Headoffice总公司
C001 C001 Electrical电气
C002 C002 Mechanical机械的
C003 C003 Civil民用

BILL table: BILL表:

BILL_NO BILL_NO DOCCLASS文档类
1 1 FC001 FC001
2 2 FC002 FC002
3 3 FC003 FC003

Description: Class is a primary key.说明: Class是主键。 Let's say we created invoices by tagging different class from the CLASS table.假设我们通过从CLASS表中标记不同的类来创建发票。

For example invoice 1 is created by tagging FCOO1 (ie. Factory and Electrical)例如,发票 1 是通过标记FCOO1 (即工厂和电气)创建的

Problem: how to write a SQL query to get only COO1 part from FC001 of Invoice 1?问题:如何编写 SQL 查询以FC001发票 1 的FC001中获取COO1部分?

This SQL code is not working:此 SQL 代码不起作用:

Select
    BILLDET.BILL_NO,
    BILLDET.CLASS,
    BILLDET.GLCODE,
    CLASS.DESCRIPT
From
    BILLDET 
Full Join 
    CLASS On BILLDET.CLASS = CLASS.CLASS

Output:输出:

Bill NO比尔没有 DESCRIPT描述
1 1 FactoryElectrical工厂电器

Thank you谢谢

Ugh, what a way to store data呃,这是一种什么方式来存储数据

WITH classx AS(
  SELECT
    c.class+n.class as class,
    c.descript+n.descript as descript 
  FROM
    class c 
    JOIN class n 
    ON 
      c.class LIKE 'c%' AND 
      n.class NOT LIKE 'c%'
)

SELECT * FROM bill JOIN classx ON bill.docclass = classx.class
select bill_no, Description
from
(
select c2.class + c1.class as keys, c2.description+c1.description as Description
from class c1, class c2
where c1.class LIKE 'c%' AND 
      c2.class NOT LIKE 'c%'
)as tab, bill
where keys = DOCCLASS;

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

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