带有计数的SQL Server Select语句

时间:2016-12-19 22:07:21

标签: sql-server tsql

我正在撰写查询以确定用户是否是我网站上的管理员。

在初步选择之后,我无法弄清楚如何显示“True”或“False”。

setDT(df)[,pattern:=rleid(x,y)]

我的IF也不起作用,可能是因为我写错了。 select count(*) as IsAdmin from UserRoles where UserID = '1' and RoleID = 1 IF count is != 0 => True ELSE False 表示他们是管理员。

2 个答案:

答案 0 :(得分:3)

您的IF中存在使用,然后继续您的逻辑/您想要返回的内容

IF(exists(select 1 as IsAdmin from UserRoles where UserID='1' and RoleID = 1))
   SELECT 'IS ADMIN'
ELSE
   SELECT 'IS NOT ADMIN'

如果在另一种情况下你试图检查某个号码,你可以这样使用它:

IF(select count(*) as IsAdmin from UserRoles where UserID='1' and RoleID = 1) > 0
   SELECT 'IS ADMIN'
ELSE
   SELECT 'IS NOT ADMIN'

答案 1 :(得分:1)

package main

import (
    "fmt"
    "net/http"
    "sync"
)

//var URL = "http://localhost:8181/api/v1/resources"
var URL = "http://httpbin.org/ip"

// Task interface
type Task interface {
    Execute(p *Pool)
}

// JobDesc struct
type JobDesc struct {
    Client *http.Client
}

// Pool struct
type Pool struct {
    mu      sync.Mutex
    size    int
    tasks   chan Task
    kill    chan struct{}
    wg      sync.WaitGroup
    Results chan interface{}
}

// Methods for Pool struct.
func (p *Pool) worker() {
    defer p.wg.Done()
    for {
        select {
        case task, ok := <-p.tasks:
            if !ok {
                return
            }
            task.Execute(p)

        case <-p.kill:
            return
        }
    }
}

// Resize methods update the number of goroutine in the pool, to execute the task.
func (p *Pool) Resize(n int) {
    p.mu.Lock()
    defer p.mu.Unlock()
    for p.size < n {
        p.size++
        p.wg.Add(1)
        go p.worker()
    }
    for p.size > n {
        p.size--
        p.kill <- struct{}{}
    }
}

// CloseJobChannel method close the jobs channel,
// once all the jobs are sending to the channel.
func (p *Pool) CloseJobChannel() {
    close(p.tasks)
}

// CloseResultChannel method close the results channel,
// once all the jobs are completed and results are collected.
func (p *Pool) CloseResultChannel() {
    close(p.Results)
}

// Wait Method of Pool struct
func (p *Pool) Wait() {
    p.wg.Wait()
}

// AddJob Method add task to the task channel
func (p *Pool) AddJob(task Task) {
    p.tasks <- task
}

// NewPool function create pool of task base on the size
// provided.
func NewPool(size int) *Pool {
    pool := &Pool{
        tasks:   make(chan Task, 10000),
        kill:    make(chan struct{}),
        Results: make(chan interface{}, 10000),
    }
    pool.Resize(size)
    return pool
}

// Execute to execute
func (job *JobDesc) Execute(p *Pool) {
    res, err := job.Client.Get(URL)
    if err != nil {
        fmt.Println(err)
    }
    //body, _ := ioutil.ReadAll(res.Body)
    fmt.Println(res.Body)
    //defer res.Body.Close()
}

func main() {
    pool := NewPool(10)
    tr := &http.Transport{DisableKeepAlives: false}
    client := &http.Client{Transport: tr}

    for i := 1; i <= 50; i++ {
        job := JobDesc{Client: client}
        pool.AddJob(&job)
    }
    pool.CloseJobChannel()
    pool.Wait()
    pool.CloseResultChannel()
    i := 1
    for k := range pool.Results {
        fmt.Printf("%d, %d", i, k)
        i++
    }
}