Interestingly none of the below questions helped me:
How to organize country/state/city browsing in MVC
City Country State Database
Country/City/state validation
Country/State/City Database?
How to do a city/state/country code lookup based on zip/country input by the user?
City, State, Zip, Country list/script for sql server table
I looked at a question about database design which is somehow helpful, but there are some questions that have been left unanswered. I have 3 tables as below:
Country ( CountryID, CountryCode, CountryName )
Region ( RegionID, RegionCode, RegionName, CountryID )
City ( CityID, CityCode, CityName, RegionID )
I have CityID in user's table. The question is what if user does not have state? How should I use dropdowns in my case?
Using ajax based textboxes is out of question in my case.
Are you trying to reach the list of countries based on cityID if so
SELECT Country.* FROM Country
LEFT JOIN Region on Region.CountryID = Country.CountryID
LEFT JOIN city on Region.RegionID = city.RegionID
WHERE city.CityID=44
would produce the country for the city. Is this what you are looking for?
The resulting country could be used to set the selected country in a dropdown.
Running a second query for regions using CityID and using the result to auto select the correct region for the city can be achieved too.
Related
How do I do a many to many (master-master) relation in Delphi, I cannot find an example. Only dblookups and master-detail which I understand.
Like a product can belong to one or more categories. Like this table structure:
Product Table
ProductId, ProductName
CategoryTable
CategoryId, CategoryName
Relation table
ProductId, CategoryId
Ideally if you select a record in the product grid the edit of a product in a detail record is started in the right side of the screen. In here you can edit the product properties an ideally you can select via checkboxes one or more categories in a checkbox group/grid.
How do you hook this up with TTable of TQuery components? Is there a way?
Hope you can help!
Sincerely Edward
Some more explaination:
The goal is like:
master [product grid list] Detail [on selected product]
property **A**
property **B**
property **C**
property **D**
property **Category collection**
Category 1 - checked
Category 2 - unchecked
Category 3 - unchecked
Category 4 - checked
This can be transformed to a simple Master-Detail relation by using a joined query over Relation Table and Category Table (similar like this example):
SELECT * FROM Relation a
JOIN Category b on a.CategoryId = b.CategoryId
WHERE ProductId = :ProductId
In case you want a list of all categories where a separate field indicates if a relation to the product exists, you can use a query like this (example for MSSQL, the new field is named Checked):
SELECT c.*, CASE WHEN r.CategoryId IS NULL THEN 0 ELSE 1 END AS Checked
FROM CATEGORY
LEFT JOIN Relation r ON (c.CategoryId = r.CategoryId) AND (r.ProductId = :ProductId)
ORDER BY c.CategoryId
Note that you have to write the code to add or delete the relation record when you manipulate the check list by yourself.
My colleague asked me a question regarding getting data from a SQL Server database.
I have a sample data set
[ID],[TOWN],[PERSON]
[1],[BELFAST],[JAMES]
[2],[NEWRY],[JOHN]
[3],[BELFAST],[SIMON]
[4],[LARNE],[ALAN]
Now from this I would like to return a SQL Dataset that returns me a different table based upon the view.
Essentially in code I could get a distinct on the town then loop sql filtering on the town. But is there a way I can do this in SQL?
Where I would get (3) views back (2 Belfast, 1 Newry and 1 Larne)
Basicly I it would return
[ID],[Town],[Person]
[1],[Belfast],[James]
[3],[Belfast],[Simon]
Then another view would return for 'Larne' and a Final one for Newry. Basically SQL creating views for each town it finds and then returns the records for each town.
You don't get views back - you have to define them yourself.
E.g. if you need one view for Belfast, a second for Newry and a third for Larne - then you need to create three views that return only those rows that match the relevant city name
CREATE VIEW BelfastView
AS
SELECT ID, Town, Person
FROM dbo.Towns
WHERE Town = 'Belfast'
CREATE VIEW LarneView
AS
SELECT ID, Town, Person
FROM dbo.Towns
WHERE Town = 'Larne'
CREATE VIEW NewryView
AS
SELECT ID, Town, Person
FROM dbo.Towns
WHERE Town = 'Newry'
Now, certain users might only be allowed to select data from the BelfastView and thus would never see any other rows of data from your underlying table.
But views are database objects like tables or stored procedures; you need to create them, maintain them, toss them when no longer needed.
EDIT
Based on your updated question, you simply need to create a view for each town you want to filter:
CREATE VIEW BelfastView AS
SELECT ID,
Town,
Person
FROM YourTable
WHERE Town = 'BELFAST'
Although you've only given us a small sample of your data, what you're asking is almost never a good idea. What happens when you have 50 new towns in your DB? Are you going to create a view for each town? This does not scale well (or at all).
Basically I have decided to Run it as a Stored Procedure to return me each item as a List. So something along the lines of this:
Create Procedure ListTowns
As
declare #towns char(11)
select #towns = (select distinct Town from [Towns])
while #towns is not null <> 0
begin
select * from [YourTable] where Town = #towns
end
I'm building an application for a University assignment, I'm using Visual Basic 2010 and Micrsoft Access for the database.
I have a form where customers can order products each product will have a set price, for example product1 is £1, however some customers will get products for cheaper so I have a table 'special prices' the special price table will contain rows including the 'customerid' 'productid' and the 'specialprice'.
I need to know how to display the price, if a customer chooses product1 I need my application to first check if they have a special price and if not just display the normal price,
Thanks,
Jack.
If Customer does not have a special price, it is null
SELECT p.Price, c.CustomerID, sp.SpecialPrice, IFNULL(sp.SpecialPrice, p.Price) As UsedPrice
FROM Product p
LEFT JOIN SpecialPrice sp ON p.ProductID = sp.ProductID
AND sp.CustomerID = 123
My answer is based on SQL Server I hope you can translate it to MSAccess. The main point is Join the Product table to special price table where current customer. If special price is available use it, otherwise use price in Product
OR in code
if(row["SpecialPrice"] != null) // if(row["SpecialPrice"] != DBNull.Value) --- instead
price = row["SpecialPrice"];
else
price = row["Price"];
i have a table named locations of which i want to select and get values in such a way that it should select only distinct values from a column but select all other values .
table name: locations
column names 1: country values : America, India, India, India
column names 2: state/Province : Newyork, Punjab, Karnataka, kerala
when i select i should get India only once and all the three states listed under India . is ther any way..??? sombody please help
You could do this:
SELECT country, GROUP_CONCAT(state SEPARATOR ', ')
FROM locations
GROUP BY country
But this sort of thing is often best done in the presentation layer.
You want it displayed in that order, not selected?
In this case you have to add a condition inside of your loop to check a country and print ot out only if it was changed.
Web app is being written in classic ASP with a MSSQL backend. On this particular page, the admin can select 1 or any/all of the employees to assign the project to. I'm trying to figure out a simple way to store the employee IDs of the people assigned to it in one column.
The list of employees is generated from another table and can be dynamic (firing or hiring) so I want the program to be flexible enough to change based on these table changes.
Basically need to know how to assign multiple people to a project that can later be called up on a differen page or from a different query.
Sorry for the n00bish question, but thanks!
Don't store multiple ID's in one column! Create another table with the primary key of your existing table and a single ID that you want to store. You can then insert multiple rows into this new table, creating a 1:m (one to many) relationship. For example, let's look at an order table:
order:
order_id
order_date
and I have a product table...
product:
product_id
product_name
Now, you could go down the road of adding a column to order that let you list the products in the order, but that would be bad form. What you want instead is something like..
order_item:
order_item_id
order_id
product_id
quantity
unit_price
You can then perform a join to get all of the products for a particular order...
select
product.*
from orders
inner join order_item on order_item.order_id = order.order_id
inner join product on product.product_id = order_item.product_id
where orders.order_id = 5
Here's an example order_id of 5, and this will get all of the products in that order.
You need to create another table that stores these values such as. So this new table would store one row for each ID, and then link back to the original record with the original records ID.