相关资料
一、 基本SELECT
语句
1.1. 带有否定操作符NOT
的SELECT
语句
1.1.1. 获取 code
不为空的所有产品
SELECT {p.pk} FROM {Product AS p} WHERE {p.code} IS NOT NULL
1.1.2. 获取 code
不包含特定字符串的所有类别
SELECT {c:pk} FROM {Category AS c} WHERE {c:code} IS NOT LIKE '%test%'
1.2. 返回多列的SELECT
语句
1.2.1. 获取所有类别的所有数据库列
SELECT * FROM {Category}
1.2.2. 获取类别上次修改的时间、code
和PK
列
SELECT {cat:modifiedtime}, {cat:code}, {cat:pk} FROM {Category AS cat}
1.3. 多个条件属性的SELECT
语句
1.3.1. 获取code
或name
包含特定字符串的所有产品
SELECT {p:PK}
FROM {Product AS p}
WHERE
{p:code} LIKE '%myProduct'
OR
{p:name} LIKE '%myProduct'
ORDER BY {p:code} ASC
1.3.2. %
用作通配符
-
a%
查找以a开头的所有字符串, -
%a
查找以a结尾的所有字符串, -
%a%
查找包含a的所有字符串。
1.3.3. 补充:通过?
占位符引入参数到查询中,可以搜索任何搜索字符串:
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:code} LIKE ?name
OR {p:name} LIKE ?name
ORDER BY {p:code} ASC
WHERE ... LIKE CONCAT('%', CONCAT(?name, '%'))
... LIKE ?name;
query.addQueryParameter("name", "%h%")
1.4. 多语言的SELECT
语句
1.4.1. 获取name
列为null
的所有产品
# 查询未明确指定语言,默认使用当前会话语言
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:name} IS NULL
1.4.2. 获取所有name
列英语null
或description
列中文为null
的产品
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:name[en]} IS NULL
OR {p:description[zh]} IS NULL
1.4.3. 同时搜索多种语言
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:description[zh]:o} LIKE '%text%'
OR {p:description[en]:o} LIKE '%text%'
# 添加`OR`子句以搜索其他属性或语言
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:description[zh]:o} LIKE '%text%'
OR {p:description[en]:o} LIKE '%text%'
OR {p:name[de]:o} LIKE '%text%'
OR {p:description[fr]:o} LIKE '%text%'
1.5. 带参数的SELECT
语句
1.5.1. 在FlexibleSearch
语句中使用一个参数
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:description[en]:o} LIKE ?param
OR {p:description[de]:o} LIKE ?param
OR {p:description[fr]:o} LIKE ?param
1.5.2. 在FlexibleSearch
语句中使用2个参数
# 获取至少属于以下两个类别之一的所有产品:
SELECT {cpr:target}
FROM {CategoryProductRelation AS cpr}
WHERE {cpr:source} LIKE ?param1
OR {cpr:source} LIKE ?param2
# 获取在两个日期之间更改的所有产品
SELECT {pk}
FROM {Product}
WHERE {modifiedtime} >= ?startDate
AND {modifiedtime} <= ?endDate
1.6. 带连接的SELECT
语句
合并多个字符串
1.6.1. 用%
括住搜索字符串
# 关于`CONCAT`操作符的示例
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:description[zh]}
LIKE CONCAT('%', CONCAT('myProduct','%'))
ORDER BY {p:code} ASC
# 结合`?`占位符的示例
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:description[zh]} LIKE CONCAT('%',CONCAT(?param,'%'))
ORDER BY {p:code}
1.7. 使用DISTINCT
操作符的SELECT
语句
清楚查询结果中的重复项
1.7.1. 清楚查询结果中的重复项
SELECT DISTINCT {cpr:target}
FROM {CategoryProductRelation AS cpr}
WHERE {cpr:source} LIKE ?param1
OR {cpr:source} LIKE ?param2
1.8. 使用GROUP BY
操作符SELECT
语句
1.8.1. 获取每个产品订单行,并按产品分组
SELECT {oe:product}
FROM {OrderEntry AS oe}
GROUP BY {oe:product}
二、 子查询
2.1. 子查询语法
SELECT *
FROM ${type}
WHERE
{{
SELECT *
FROM ${other_type}
WHERE ${subselect_search_condition}
}}
2.2. 对多种类型进行子选择
2.2.1 获得每一个直接或间接指定价格的产品
SELECT DISTINCT {p:PK}, {p:name}, {p:code}
FROM {Product AS p}
WHERE {p:PK} IN
(
{{
-- subselect 1
SELECT {dr:product} FROM {DiscountRow AS dr}
}}
)
OR {p:PK} IN
(
{{
-- subselect 2
SELECT {prod:PK}
FROM {
Product AS prod
LEFT JOIN DiscountRow AS dr ON {prod:Europe1PriceFactory_PDG} = {dr:pg}
}
WHERE {prod:Europe1PriceFactory_PDG} IS NOT NULL
}}
)
ORDER BY {p:name} ASC, {p:code} ASC
2.2.2 获得至少属于3个类别的所有产品
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:PK} IN
(
-- subselect 1
SELECT prod
FROM
(
{{
-- subselect 2
SELECT {cpr:target} AS prod, count({cpr:target}) AS howmany
FROM {CategoryProductRelation AS cpr}
GROUP BY {cpr:target}
}}
) temptable
WHERE howmany > 3
)
ORDER BY {p:name} ASC, {p:code} ASC
2.2.3 结合?
占位符的示例
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:PK} IN
(
-- subselect 1
SELECT prod
FROM
(
{{
-- subselect 2
SELECT {cpr:target} AS prod, count({cpr:target}) AS howmany
FROM {CategoryProductRelation AS cpr}
GROUP BY {cpr:target}
}}
) temptable
WHERE howmany > ?number
)
ORDER BY {p:name} ASC, {p:code} ASC
2.3. 使用参数进行子选择
2.3.1 在指定日期和指定时间之后被订购所有产品
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:PK} IN
(
{{
-- subselect 1
SELECT DISTINCT {oe:product}
FROM {OrderEntry AS oe}
WHERE {oe:order} IN
(
{{
-- subselect 2
SELECT {o:PK}
FROM {Order AS o}
WHERE {o:date} >= ?date
}}
)
}}
)
2.3.2 获取指定货币中没有标价的产品
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:PK} NOT IN
(
{{
-- subselect
SELECT {pr:product}
FROM {PriceRow AS pr}
WHERE {pr:currency} = ?currency
}}
)
ORDER BY {p:name} ASC, {p:code} ASC
# 也可以用 不存在`NOT EXISTS`
SELECT {p:PK}
FROM {Product AS p}
WHERE {p:PK} NOT EXISTS
(
{{
-- subselect
SELECT {pr:product}
FROM {PriceRow AS pr}
WHERE {pr:currency} = ?currency
}}
)
ORDER BY {p:name} ASC, {p:code} ASC
使用FROM
子句和SQL
聚合函数并带有子查询的报表查询
SELECT AVG(torderentries.totprice), AVG(torderentries.totquantity)
FROM
(
{{
SELECT SUM({totalPrice}) AS totprice, SUM({quantity}) AS totquantity
FROM {OrderEntry}
WHERE {creationtime} >= ?startDate
AND {creationtime} < ?endDate
GROUP BY {order}
}}
) AS torderentries
2.4. 将SELECT
语句与UNION
操作符组合在一起
UNION
操作符用于合并两个或多个SELECT
语句的结果集。
2.4.1. 语法
SELECT x.PK
FROM
(
{{
SELECT {PK} AS PK FROM {Chapter}
WHERE {Chapter.PUBLICATION} LIKE 6587084167216798848
}}
UNION ALL
{{
SELECT {PK} AS PK FROM {Page}
WHERE {Page.PUBLICATION} LIKE 6587084167216798848
}}
) x
2.4.1 获取订单的所有支付金额
SELECT result.orderid, result.name, result.amount
FROM
(
{{
SELECT {o.orderid} AS orderid, {op.payname} AS name, {op.payAmount} AS amount
FROM
{
OrderPayType AS op JOIN Order AS o ON {op.order ={o.pk}
}
WHERE {o.orderid} = '10001'
}}
UNION
{{
SELECT {o.orderid} AS orderid, {opi.name} AS name, {opi.promotionAmount} AS amount
FROM
{
OrderPromotionInfo AS opi
JOIN order AS o ON {o.pk}={opi.order}
}
WHERE {o.orderid} = '10001'
}}
) result