Skip to content

Latest commit

 

History

History
60 lines (50 loc) · 1.32 KB

in_clause.md

File metadata and controls

60 lines (50 loc) · 1.32 KB

The following query in select-products-in-categories.sql.

SELECT 
    ProductID, 
    ProductName, 
    UnitPrice
FROM Products
WHERE Discontinued = false
    AND CategoryID IN (?)

Will generate the following types and functions.

export type SelectProductsInCategoriesParams = {
    categories: number[];
}

export type SelectProductsInCategoriesResult = {
    ProductID: number;
    ProductName: string;
    UnitPrice?: number;
}

export async function selectProductsInCategories(client: Client, params: SelectProductsInCategoriesParams) : Promise<SelectProductsInCategoriesResult[]> {
    const sql = `
    SELECT 
        ProductID, 
        ProductName, 
        UnitPrice
    FROM Products
    WHERE CategoryID IN (?)
    `

    return client.query(sql, [params.categories])
        .then( res => res );
}

And you can use the API as below.

const products = await selectProductsInCategories(client, {
    categories: [10, 11, 12]
})

You can also use the NOT IN Clause:

SELECT 
    ProductID, 
    ProductName, 
    UnitPrice
FROM Products
WHERE Discontinued = false
    AND CategoryID NOT IN (98, 99, ?)

NOTE: The IN Clause as shown will not work with the deno_mysql driver. See issue #70 in the deno_mysql repository.