sql

SQL - How to select a row having a column with max value

date value 18/5/2010, 1 pm 40 18/5/2010, 2 pm 20 18/5/2010, 3 pm 60 18/5/2010, 4 pm 30 18/5/2010, 5 pm 60 18/5/2010, 6 pm 25 i need to query for the row having max(value)(i.e. 60). So, here we get two rows. From that, I need the row with the lowest time stamp for that day(i.e ...

Does MySQL allow two primary keys on one table?

CREATE TABLE Orders -> ( -> ID SMALLINT UNSIGNED NOT NULL, -> ModelID SMALLINT UNSIGNED NOT NULL, -> Descrip VARCHAR(40), -> PRIMARY KEY (ID, ModelID) -> ); Basically, this appears to me to be creating two primary key on one table. Is that correct? I thought that we could create a number of unique keys in one table, but on...

SQLInForm.com like plug-in for SQL Server 2008

Is there a plug-in like the java applet @ SQLinForm.com for MS SQL Server Management Studio 2008? I tried SSMS tools based on someone's answer to my previous question but I don't find any functionality to format (beautify/tabify) SQL. If somebody could please point out that in SSMS tools or suggest some other tool (preferably free) T...

How can i test my TSQL syntax?

Quick question: How do I get some kind of database to use to test my SQL syntax and create basic data. I have Sqlite Code which I'll soon put on a server. I have SQL Server 2008 installed with visual studio 2010. I tried connecting to the database and had no luck. I also tried using an .mdf file instead thinking it's a file and I won...

Returning several COUNT results from one ASP SQL statement

Say I have a table like this: Field1 Field2 Field3 Field4 fred tom fred harry tom tom dick harry harry and I want to determine what proportion of it has been completed for each field. I can execute: SELECT COUNT (Field1) WHERE (Field1 <> '') AS Field1Count SELECT COUNT (Field2) WHERE (Fi...

How can I use a php array in a mysql search query?

I was going to use the scuttle solution on: http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html for handling searches on my website. I was wondering how I could take the search input from a user and turn it into a single query. For instance, let's say a user inputted 'blue dogs' in their search query... How could I dyna...

sql jdbc getgeneratedkeys returns column "id" not found, column type unknown

I want to retrieve the most recently updated value in the table using an insert query. these are the datatypes in my sql table. int(11) // primary key auto increment, not being assigned by sqlQuery varchar(30) timestamp // has a default value. but i am explicit assigning it using CURRENT_TIMESTAMP varchar(300) varchar(300) varcha...

Why cant i use the field user in SQL Server 8?

Maybe not literally but the query below gets an error near user. If i change it to userZ it works. WHY can i not use that name? Is there a way to specific its a field instead of a keyword? (or whatever it is) create table Post2 ( id INTEGER PRIMARY KEY NOT NULL, title nvarchar(max) NOT NULL, body nvarchar(max) NOT NULL, user integer R...

like operator issue

I have dom table select * from dom dom table details: id name skills 1 dom c,c++ Here I want to retrieve query using like operator select * from dom where skills like '%c,c++%' Then I got the desired result.....that's not a problem. Suppose I want to use the below query `select * from dom where skills like '%C+...

SQL Server Select Distinct

I want to write a query like this: For a table that has these columns: ColA ColB ColC, ColD select first(ColA, ColB, ColC, ColD) distinct(ColB, ColC) from table order by ColD The query is supposed to order the table by ColD, then group the results by the combination of ColB and ColC (they may have different data types) and returns the...

Specify which row to return on SQLite Group By

I'm faced with a bit of a difficult problem. I store all the versions of all documents in a single table. Each document has a unique id, and the version is stored as an integer which is incremented everytime there is a new version. I need a query that will only select the latest version of each document from the database. While using GR...

set sql server connection to readonly?

How do I set a SQL server connection to readonly? I tried Googling and all I found was File Mode=Read Only, but it didn't work (File Mode keyword not supported). The reference looked SQL CE specific. No luck with SQLite Read Only=True either. -edit- My connection string is below. I have no clue when it comes to configuring the tables....

Codeigniter: Using Active Record to search for rows between two dates?

Hi There, Is there a way to use codeigniters active record system to construct a query that will search for rows that fall between two dates (rows has an added field and I want to supply the query a start and end date)? I have checked the documentation knowing that mysql includes a between keywork for such things but can't see anything...

Can I use EXISTS keyword in sql query for assignment of result

Can I assign a value with EXISTS in the query. Currently it is giving me error Incorrect syntax near '='. But when I uses this query only it runs as expected. IF EXISTS (SELECT @PK_LOGIN_ID=PK_LOGIN_ID FROM dbo.M_LOGIN WHERE LOGIN_NAME=@LOGIN_NAME AND PASSWORD=@PASSWORD AND FK_ROLE_ID=@FK_ROLE_ID) RETURN @PK_LOGIN_ID ...

PHP PDO fetch null

How do you check if a columns value is null? Example code: $db = DBCxn::getCxn(); $sql = "SELECT exercise_id, author_id, submission, result, submission_time, total_rating_votes, total_rating_values FROM submissions LEFT OUTER JOIN submission_ratings ON submissions.exercise_id=submission_ratings.exercise_id WHERE id=:id"; $st = $db->p...

Check if a connection is in a transaction

I am getting a SqlConnection does not support parallel transactions. exception and this answer mentions its when a connection tries to open two transactions. This is exactly what i am doing. I thought nested transactions were ok (i was using sqlite for the prototype). How do i check if the connection is already in a transaction? I am us...

PostgreSQL cross server query?

Is there a way that I might query a database located on "Server 2" and get my data in "Server 1" ? That is return a set of records from a remote server to my local one. PS: Not cross database query on same server because I know how to do that with dblink. Update: great ty. http://www.postgresonline.com/journal/index.php?/archives/44-...

Why I am not able to update the column based on a condition which is not the primary key

Why I am not able to update the column based on a condition which is not the primary key. I am trying to update the constituencies table where name matches a specific criterial as shown below but the below queries shows an error Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that correspond...

SQL Latest photos from contacts (grouped by contact)

Hello, To short version of this question is that I want to accomplish something along the lines of what's visible on Flickr's homepage once you're logged in. It shows the three latest photos of each of your friends sorted by date but grouped by friend. Here's a longer explanation: For example I have 3 friends: John, George and Andrea. ...

looking for a good free online tutorial on sql

Looking for a good tutorial for the complete beginner on sql. I need to know how to start up a development server as well. thanks ...