sql

How to do this join query in Django

In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories = models.ManyToManyField(Category) class ProductRank(models.Model): product = models.ForeignKey(Product) rank = models.IntegerField(default = 0) I put the rank into a separate table because every view of a pa...

Examples of SQL Injections through addslashes()?

In PHP, I know that mysql_real_escape in much safer than using addslashes. However, I could not find an example of a situation where addslashes would let an SQL Injection happen. Can anyone give some examples? Thanks! ...

Can you replace or update an SQL constraint?

I have written the following constraint for a column I've called 'grade': CONSTRAINT gradeRule CHECK grade IN (‘easy’, ‘moderate’, ‘difficult’), Is it possible to later update the gradeRule to have different values? For example, 'moderate' and 'difficult' could be changed to 'medium' and 'hard'. Thanks ...

How do I use the results of a stored procedure from within another?

I have a stored procedure that I want to call from within another, and then loop through the results. Sort of like using a cursor with a stored procedure rather than a SQL select statement. I can't quite figure out how to do it. I can get the whole result like this: DECLARE @result int; EXEC @result = sp_who; PRINT @result; Interesti...

Summing the values from the 2nd table based on ID of the 1st table

I have done this query before, but for some reason I always have to dig the answer up. Can someone explain the solution for me so I can finally 'get it'! (thanks!) Table#1 Employees (employeeID, username) Table#2 Sales (saleID, employeeID, amount) Question: List all the employees, along with the total # (count) of sales they have. ...

When is an index (in a DBMS) a bad index?

Can anyone tell me when an index is a bad index? ...

When should I open and close a connection to SQL Server

I have a simple static class with a few methods in it. Each of those methods open a SqlConnection, query the database and close the connection. This way, I am sure that I always close the connection to the database, but on the other hand, I don't like to always open and close connection. Below is an example of what my methods look like. ...

MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET

What is main difference between INSERT INTO table VALUES .. and INSERT INTO table SET? Example: INSERT INTO table (a, b, c) VALUES (1,2,3) INSERT INTO table SET a=1, b=2, c=3 And what about performance of these two? ...

Generating SQL upgrade scripts for the customer

What is the best way to maintain upgrade scripts between versions of a product? If a customer starts with version 3 of your product and goes to version 5, what is the best way to generate an upgrade script for the customer so that any differences in the database schema between versions 3 and 5 are resolved? ...

How to avoid the "divide by zero" error in SQL?

I hate this error message: Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered. What is the best way to write SQL code, so that I will never see this error message again? I mean, I could add a where clause so that my divisor is never zero. Or I could add a case statement, so that there is a special treatment for zer...

How can i evaluate this query?

This query give me error:Msg 156, Level 15, State 1, Line 10 Incorrect syntax near the keyword 'where'. declare @date1 datetime,@date2 datetime , @COUNT INT , @countgap int, @order int seLECT @date1='2009-05-11' , @date2 = '2009-05-12' seLECT @countgap = 30 , @COUNT = 0, @order=23 select VisitingCount from ( select count(page) as Visi...

How to search for matches with optional special characters in SQL?

I have a problem with a search query I am using my data contains names and information that has apostrophes in various forms (HTML encoded and actual). So for example I would like an alternative to this: SELECT * FROM Customers WHERE REPLACE(LastName,'''','') LIKE Replace('O''Brien,'''','') This is just an example, what I want is a...

Combining a select statement of boolean

Hi guys could you please help me out here. SELECT e.name, IF e.active = 0 THEN e.active = true WHERE e.id = #{estate_id} ELSE e.active = false WHERE e.id = #{estate_id} END IF AS Estate_status FROM estates e ...

Querying for Consecutive Rows with Certain Characteristics

I've got a table with the following columns: id int(10) user int(10) winner int(10) profit double created datetime The winner column can be either 0 or 1. I'd like to create a query that returns the maximum number of consecutive winners as ordered by the created datetime column along with the first and last created date as well as th...

SQL Help With Query - Group By and Aggreate Functions

Hi guys, I have a query I need some help with, have been checking out a fair few tutorials but nohting I found covers this issue. I have three joined tables, Products,ProductImagesLookUp and Images. A product can have any number of Images and the Order of the Images for a Product are stored in ProductImagesLookUp. I need to return...

select to group rows

i have a table like this : Alternative |Total |Male |Female a |20 |10 |10 b |50 |20 |30 c |40 |10 |30 c |30 |15 |15 now i want to select all the rows and the "c" alternative to be grouped. ...

How can I join two queries?

How can i join these two queries (Query1-Query2) Query1: declare @date1 datetime,@date2 datetime , @COUNT INT , @countgap int seLECT @date1='2009-05-11' , @date2 = '2009-05-12' seLECT @countgap = 30 , @COUNT = 0 select @date1 TARIH , @COUNT SIRA INTO #TMP WHILE @date1 < @date2 BEGIN SELECT @date1 = DATEadd(minute,@countgap...

SQL query does not use available index (SQL Server 2008)

I have the following table in SQL Server 2008: Session ( sessionid varchar(10) startdate dateteime enddate dateteime --rest of the fields go here ) I have the following two nonclustered indexes created: Inddex1: SessionID,startdate,enddate Inddex2: startdate,enddate I have the following query select * from session where startdate>...

How much of a page is occupied by nvarchar(X)?

What are the storage requirements for nvarchar(X)? So for example, if the value in a column is much smaller than X, how much is actually stored in the database page? ...

Why is this query faster with multiple selects rather than using between?

I have a table in Sql Server 2008 Express which contains 18 million records. The structure looks something like this (simplified): Id, GroupId, Value, Created Id is the primary key with a clustered index GroupId is a non-clustered index In this case, every 10 rows get a new groupId meaning that records 1-10 have GroupId 1, records 11-...