Npgsql 5 a single select and multiple arguments - c#

I use Npqsql 5. Is there any way to retrieve values for many different arguments using a single select?
For example I want to run:
"select value from table where id=#id1"
"select value from table where id=#id4"
"select value from table where id=#id12"
but as a single query in order to optimize.

Couple of options that come to mind:
select value from table where id = #id1 or id = #id4 or id = #id12
or
select value from table where id in (#id1, #id4, #id12)

Related

How to get the count of occurrences in one table and update another table to that value?

I want to write an SQL statement that will:
Count the number of rows in the table booking that are open and where the booking.postcode is "MK"
Take a note of the plot (in booking.plot_id), and then update the table plot.jobs with the value count
For example running the SQL query when booking table has the following rows:
Would see the following highlighted values in plot.jobs being updated to 1:
This my code so far (note I am using Connector/Net):
public int CountBooking()
{
string query = "SELECT Count(*) FROM booking WHERE postcode=MK AND status=open";
int count = -1;
// ExecuteScalar will return one value
var cmd = new MySqlCommand(query, _connection);
count = int.Parse(cmd.ExecuteScalar() + "");
// Close
CloseConnection();
return count;
}
If there were 3 rows with a plot_id of 4, and 6 rows with a plot_id of 6, then the highlighted values will be updated to 3 and 6 respectively.
How would I go about achieving it?
You are close on the first part. If you want the number of openings for each plot_id, you'll need to use that in your GROUP BY statement like this:
SELECT plot_id, COUNT(*) AS numOpenings
FROM bookings
WHERE postcode = 'MK' AND status = 'open'
GROUP BY plot_id;
You can use that as a subquery in your UPDATE statement by joining it to the plot table and updating the matching rows, like this:
UPDATE plot p
JOIN(
SELECT plot_id, COUNT(*) AS numOpenings
FROM bookings
WHERE postcode = 'MK' AND status = 'open'
GROUP BY plot_id) temp ON temp.plot_id = p.plot_id
SET p.jobs = temp.numOpenings;
This worked out in SQL Fiddle. Let me know if you have more problems.
Never used Connector/Net but I would do something like this:
Run this query to retrieve the plot_id and JobCount:
select plot_id, count(*) JobCount from booking where postcode = 'mk' and status = 'open' group by plot_id;
Then scroll through this result set and issue update commands like this:
Update Jobs set jobs = [JobCount] where plot_ID = [plot_id]
Note the values of [JobCount] and [plot_id] will come from your first query.

Issue Related to where clause in SQL Server

I have a table name Students having (studentId, StudentName,Address,PhoneNo)
i have provided a filter for user to select only StudentId from Combobox to get the details of Students... and generate Report.
I have written following Query to get student Detail :
(select * from Students where StudentId = stdId)
Here stdId is a Parameter that i pass from code
It works fine if i select single studentId.... But in user selection Comobobox i have also provided "ALL" if user Select All from combobox i want to display details of all student
So what should I pass in stdId if user selects All ?
I used inline Query in C# (not using SQL Stored Procedure)
You can do it like this .
SELECT * from Students s
WHERE s.studentId = ISNULL(#StudentId,s.studentId)
When "All" is selected in combo box pass null in your #studentid parameter. If you can not pass null in parameter then pass -1 or anything which can not be contain in you combox option and do it like this:(if -1= All)
SELECT * from Students s
WHERE s.studentId = #StudentId or #StudentId=-1
You can also try the answer given by Curt.
If user selects all, pass NULL to #StudentId and change your query to:
select *
from Students
where (StudentId=#StudentId OR #StudentId IS NULL)
In your stored procedure header, change the definition of #StudentId so that it can be passed as NULL.
ALTER PROCEDURE dbo.WhateverYourProcedureIsCalled( #StudentId int = null )
Then change your WHERE clause as follows
...
SELECT * from Students
WHERE #StudentId IS NULL OR StudentId = #StudentId
From your code, if ALL is passed, you can omit the part where you set the value of the #StudentId parameter. SQL Server will use the default if not passed.
If the where clause is included in the query you also need to supply a valid parameter value and it is therefore not possible to get all students when the where clause is included.
You need to differentiate your query based on the value selected in the combobox. You can do something like the following.
int studentId = 0;
//Where selectedValue comes from your combobox
Int32.TryParse(selectedValue, out studentId);
var query = "select * from Students";
if (studentId > 0)
{
query = query + " where StudentId = #StudentId";
//Remember to add studentId as parameter value
}

How to get Sum from two tables?

i have two tables first one name is "sales" and second one name is "items"
in both tables have same columns "code" and " qtd ";
i want write MYSQL query witch i need sum(qtd) from both of tables where code is same in both of tables .
for single table i am using this
"select code,sum(qtd) from sales group by code";
Try this:
Select code, sum(qtd)
from (
select code, qtd from sales
union all
select code, qtd from items) as innerTable
group by code

INSERT with SELECT DISTINCT and another field included?

I have a products table and each product have a category field (this is not up to me to change and I need to read the data from it as it is), from the product table I wanted to update a category table with unique categories only (I also use a IFNULL to deal with empty categories), this is what I have so far that works:
string query = "INSERT INTO categories (name)
SELECT DISTINCT(IFNULL(category, \"Uncategorized\")) AS category_name
FROM products
WHERE NOT EXISTS (
SELECT name FROM categories
WHERE name = category_name)";
With the above query I am able to update the categories with unique category fields and every time I need to run it, it will add only categories that does not exist to the table.
But besides the above I also need to include a profile id with each category inserted something like this, which does not work:
string query = "INSERT INTO categories (profile_id, name)
SELECT " + profileId.ToString() + ",
DISTINCT(IFNULL(category, \"Uncategorized\")) AS category_name
FROM products
WHERE NOT EXISTS (
SELECT name FROM categories
WHERE name = category_name
AND profile_id = #profileId)";
Is there a way I could acomplish this with a query ?
The above gives me a message:
SQLite error near "DISTINCT": syntax error
SqliteMan error:
Query Error: near "DISTINCT": syntax error Unable to execute statement
Let me know if I havent been clear and what should I be more clear about.
You are using the DISTINCT keyword incorrectly: it is not a function. Try instead:
"INSERT INTO categories (profile_id, name)
SELECT DISTINCT '"+profileId.ToString()+"', IFNULL(category, 'Uncategorized')
FROM products
WHERE category_name NOT IN (
SELECT name FROM categories WHERE profile_id = "'+profileId.ToString()+"'
)
"
One assumes that profileId.ToString() is guaranteed to be safe from SQL injection, or else you would be escaping it/passing it as a parameter to a prepared statement?

Select from two tables inside an insert statement in sql

i am trying to insert a statement contains WHERE from two different tables :
the table i want to insert into is dbo.order
the other two tables are :
dbo.users. user_id.
dbo.packages. package_id.
another order field "notes".
the statement i tried is
insert into dbo.order
(customer_id,package_id,notes)
Select user_id,Package_ID
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner','notes value here';
any suggestions ?
There's no obvious join here so you'll get the cartesian product of Bader orders and beginner packages. Not sure what the notes value should be. If its a literal you can just include it in the select clause.
insert into dbo.order
(customer_id,package_id,notes)
Select
user_id,Package_ID , 'notes value here'
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner';
insert into dbo.order
(customer_id,package_id,notes)
Select user_id, Package_ID, 'notes value here'
from
dbo.users, dbo.packages
where
username = 'bader' AND package_name = 'beginner';

Categories