簡體   English   中英

使用MySQL進行復雜的SQL查詢?

[英]Complex SQL Query with MySQL?

我有一個數據庫:

[business] must have a [location]
[location] may have many [business]
[location] may have many [postcode]
[postcode] may be for many [location]
[postcode] must have a [state]

我希望查詢返回每個州的業務計數最高的10個位置。

為了更有意義,它可能會出來:

Western Australia
  - perth
  - bunbury
  - etc. up to 10

(即,珀斯在西澳大利亞州列出的企業最多)

Victoria
  - melbourne
  - st kilda
  - etc. up to 10

等等。

我希望在不使用UNION的情況下實現這一目標。 我有一段時間沒有做復雜的SQL了,這傷了我的頭。

我正在使用MySQL。

如果您具有類別,產品和訂單,並且希望按每個類別的訂單數來排名前十位的產品,則可以采用類似的方法。

此查詢應該工作。

select * from (
    select a.state, a.location_id, C,
        @n:=case when @s=a.state then @n+1 else 1 end counter,
        @s:=a.state
    from (select @s:=null) b, (
        select pc.state, b.location_id, COUNT(b.location_id) C
        from postcode pc
        inner join location_postcode lp on lp.postcode_id=pc.id
        inner join business b on b.location_id=lp.location_id
        group by pc.state, b.location_id
        order by pc.state, C desc
    ) a
) c
where counter <= 10

我使用了易於理解的字段名稱,假設這些表以規定的關系存在:

business M-1 location
location M-M postcode
    (expands to) => location 1-M location_postcode M-1 postcode
postcode M-1 state

查詢已使用以下數據進行了測試:

create table business (location_id int);
insert into business select floor(rand()*10);
insert into business select floor(rand()*10) from business;
insert into business select floor(rand()*10) from business;
insert into business select floor(rand()*10) from business;
insert into business select floor(rand()*10) from business;
insert into business select floor(rand()*10) from business;
insert into business select floor(rand()*10) from business;
insert into business select floor(rand()*10) from business;
insert into business select floor(rand()*10) from business;
insert into business select floor(rand()*10) from business;
create table location_postcode (location_id int, postcode_id int);
insert into location_postcode select 1,1;
insert into location_postcode select 2,1;
insert into location_postcode select 3,1;
insert into location_postcode select 4,2;
insert into location_postcode select 5,1;
insert into location_postcode select 5,2;
insert into location_postcode select 6,1;
insert into location_postcode select 6,3;
insert into location_postcode select 7,1;
insert into location_postcode select 7,4;
insert into location_postcode select 8,5;
insert into location_postcode select 9,6;
insert into location_postcode select 10,7;
create table postcode (id int, state int);
insert into postcode select 1,1;
insert into postcode select 2,2;
insert into postcode select 3,3;
insert into postcode select 4,4;
insert into postcode select 5,4;
insert into postcode select 6,5;
insert into postcode select 7,5;

這並不能為每個“前10名”創建足夠的記錄,但是您將看到COUNTER列的排名正確。 要查看它是否適用於此較小的數據集,請首先將該過濾器留在此處

where counter <= 10

檢查COUNTER列,然后將其減少為2或3,僅顯示每個州的前2或3位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM