简体   繁体   English

MySql 在单个查询中多次选择

[英]MySql multiple selects in a single query

I have a restful web service that has an endpoint for getting all sub-resources of a single resource (say, for a contrived example, selecting all pets for a certain pet store by making a GET request to http://www.url.com/stores/1234/pets ).我有一个宁静的 Web 服务,它有一个端点,用于获取单个资源的所有子资源(例如,对于一个人为的示例,通过向http://www.url发出 GET 请求来选择某个宠物商店的所有宠物。 com/stores/1234/pets )。

This web service is highly concurrent, and gets hit with many requests at once.此 Web 服务是高度并发的,并且会同时收到许多请求。 I'd like to write a single query (for a MySQL database) that allows me to retrieve the number of pets associated with a store, and to be able to distinguish if a store with that id doesn't exist.我想编写一个查询(用于 MySQL 数据库),它允许我检索与商店关联的宠物数量,并能够区分具有该 ID 的商店是否不存在。

That is, if a store with that id exists, but there are no pets from that store, it should return one thing (an empty list);也就是说,如果存在具有该 id 的商店,但该商店没有宠物,则它应该返回一件事(空列表); but if that store id does not exist, then raise an exception.但如果该商店 ID 不存在,则引发异常。 Can someone please help me write this SQL query?有人可以帮我写这个 SQL 查询吗?

Here is some pseudo-sql这是一些伪sql

CREATE TABLE STORE(
STORE_ID <- AUTO GENERATED PK
.....
)

CREATE TABLE PET(
PET_ID <- AUTO GENERATED PK
STORE_ID <-FOREIGN KEY POINTING TO STORE TABLE
)

STORE|PET
1    | 1,2
2    | NO PETS

I would want the query to be able to give different outputs for querying for store id 2 (where the store exists, but no pets associated with it) and querying for pets for store 3 (where no store with id 3 exists).我希望查询能够提供不同的输出来查询商店 id 2(商店存在,但没有与之关联的宠物)和查询商店 3 的宠物(不存在 id 3 的商店)。

Please avoid multiple queries (even using locks/transactions) or stored procedures if possible.如果可能,请避免多次查询(甚至使用锁/事务)或存储过程。

Additional notes:补充说明:
For the web service, at first I had made a query to check that a store exists with that id, if it doesn't, then throw an exception (that turns into a 404).对于 Web 服务,起初我进行了查询以检查是否存在具有该 ID 的商店,如果不存在,则抛出异常(变成 404)。 However, in a highly concurrent application, we could check that the store exists, and find that it does.然而,在一个高度并发的应用程序中,我们可以检查 store 是否存在,并发现它确实存在。 However, there could be another request that deletes that store before our next query (to retrieve all pets) occurs.但是,在我们的下一个查询(检索所有宠物)发生之前,可能会有另一个请求删除该存储。 In this case, I would like to be true to RESTful principles and return a 404, rather than an empty list.在这种情况下,我想忠于 RESTful 原则并返回 404,而不是空列表。 Hence, I would need to distinguish between the two cases.因此,我需要区分这两种情况。 I know how to do this with a transaction and a lock (select for update), but I'd prefer to do this another way if possible.我知道如何使用事务和锁(选择更新)来做到这一点,但如果可能的话,我更愿意用另一种方式来做到这一点。

select s.store_id, count(p.store_id) 
from store as s 
left join pet as p -- get a row if the store has no row in PETS
  on s.store_id = p.store_id
where s.store_id = 9
group by s.store_id -- get no row even if store doesn't exist.

The LEFT join assures that the store is returned even if it has no rows in PETS, the count(p.store_id) returns 0 (caution: don't use count(s.store_id) , this would result in 1). LEFT 连接确保即使在 PETS 中没有行也返回商店, count(p.store_id)返回 0(注意:不要使用count(s.store_id) ,这将导致 1)。

And the GROUP BY utilizes the fact that a COUNT with Group By returns no result if there's no input row (caution: without Group By the Count will return a row).并且 GROUP BY 利用这样一个事实:如果没有输入行,带有 Group By 的 COUNT 不会返回结果(注意:没有 Group By the Count 将返回一行)。

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

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