sql

MySQL skip one child level

I have a table that has location ids in parent child relationships. It is basically a hierarchy like: State -County --City The table maps the hierarchy with the following columns parentID childID type If I want to get all sub-locations for a given location that's straightforward, something like: SELECT childID FROM locatio...

Select BETWEEN column values

I'm trying to use the BETWEEN with column names instead of direct values, something like this: SELECT * FROM table WHERE column1 BETWEEN column2 AND column3; This is returning something like 17 rows, but if i write: SELECT * FROM table WHERE (column1 <= column2 AND column1 >= column3) OR (column1 >= column2 AND column1 <= column3) ...

Selecting columns in UNION

$queryActivities = mysql_query(" SELECT ua.status, ua.date, 'status' AS is_table FROM users_statuslog ua WHERE ua.uid = '{$showU[id]}' UNION ALL SELECT us.message, us.date 'wall' FROM users_wall us WHERE us.uid = '{$showU[id]}' ORDER BY `date` DESC"); This is what I have right now. And I need some more columns from users_wall, ...

What is the best way of define "ENUM" data type when creating a table in Oracle 10?

For example: I want a data type called season (= { spring, summer, fall, winter} ...

Best paging solution using SQL Server 2005?

What is the most efficient paging solution using SQL Server 2005 against a table with around 5,000-10,000 rows? I've seen several out there but nothing comparing them. ...

Relational database design question - Surrogate-key or Natural-key?

Which one is the best practice and Why? a) Type Table, Surrogate/Artificial Key Foreign key is from user.type to type.id: b) Type Table, Natural Key Foreign key is from user.type to type.typeName: ...

Do I need to model a dependency explicitly in an ER diagram?

I was looking at ER diagrams today. If we consider two entities, Item and Member in the context of a rental shop, the member can either checkout an item or renew the item. So for this, I came up with the following: The idea was that a member can check out any number of items but an item can be checked out only once. And, a member can ...

Return 1 result per left join

Currently I am performing a left join on two tables. The first table has an id and a persons name, the second table has an id, the id of a person from table 1, and then a timestamp (of a flight). People Flights id | name id | person_id | time ------------ ---------------------...

Database Design Foreign Keys

Two tables in my database are as follows: [Employee] Table: Id (Primary Key, Autoincrement) FirstName LastName Status [Status] Table: Status (Primary Key) Status is one of the following: "FullTime" "Contractor" "Terminated" How should [Employee].Status reference [Status].Status as foreign key? I see two ways of doing this: [E...

How do I count MySQL entries with a HAVING criterion

So usually you can just do SELECT COUNT(field.id) FROM table WHERE field.id > 100 and COUNT(field) will return the number of entries that has the criterion of field.id > 100 But then what if you what to count entries specified with the HAVING criterion such as SELECT COUNT(field.id), field.id * 10 AS foo FROM table HAVING foo > 100...

Getting the SQL from a Django QuerySet

How do I get the SQL that Django will use on the database from a QuerySet object? I'm trying to debug some strange behavior, but I'm not sure what queries are going to the database. Thanks for your help. ...

how to use web service search/get methods

i would like to be able to retrieve results from this search by connecting directly to their database instead of screen-scraping: http://broinc.com/search.php would it be possible to return the results programmatically into some variable, instead of manually doing this search and then going through every page to get the results? ...

Search result in presented in specific order?

SELECT id FROM films WHERE id IN (90,4,40) result 4 40 90 but I want it return 90,4,40 what I must do? (mysql) ...

SQL query to remove left and right characters in a field

I have a sql field that i need to edit out characters on the left and right. The table name is: cdr The field name is: dstchannel The dstchannel field shows the following data: Local/872@from-internal-6acb,1 I just the need the 872 as output The field always has the same amount of data, so the 872 will be constaint on its position. How w...

Delete Records with SQL

Hi All, I'm stuck with this problem I need help. I have 3 tables in MSAcess as follows: Table1 has two columns a JN and SN, Table2 and Table3 only have one column a SN. On Table1 the JN is allowed to have duplicates. What I would like to do is to delete the JN and all SN associated with the JN. So for example if Table1 SN contain th...

Is this a good place for a clustered index for ms sql 2008?

I have a database table that stores the file path of a file and a revision number. Each file has a revision number associated with it. Each file has about 10 revisions at any given time. Every day a new revision gets created for every file Every day the oldest revision gets removed for every file. There are about 100 Million file...

I am writting a mysql query. Help me out.

I have a typical situation. my table has four column. (id, user, col2, status) I want to write a query which gives me the results of col2. But it has a column status which has (0/1). So I want only col2 data which has 0 status + a user's all data (0/1). id user col2 status 1 sam aa 1 2 sam bb 0 3 sam cc 1 4 max dd 0...

Individual index size for SQL Server

I can use sp_spaceused tablename to get the total index size for that table. But is there a way to get all indexes of that table's size individually? ...

How to make SQL rise an event at SQL Client

I need to create ConsoleApp in C# which is going to write something on local disk of client corresponding of data added to SQL Server by others. Now I do not know is there a way to SQL rise an event at client. My idea is to periodicity check for new data by opening connection and selecting data. Before I start coding Ill like to explore ...

Display SQL query results

I am having trouble displaying results from a SQL query. I am trying to display all images and prices from a products table. I am able to display the echo statement "Query works" in the browser. But, the results are not displaying in the browser. if ($count > 0) { echo "Query works"; } else { ec...