Click here to Skip to main content
15,897,518 members
Home / Discussions / Database
   

Database

 
GeneralSQL Server Access rights Pin
Nick Seng10-Mar-04 17:48
Nick Seng10-Mar-04 17:48 
GeneralRe: SQL Server Access rights Pin
Mike Dimmick11-Mar-04 2:29
Mike Dimmick11-Mar-04 2:29 
GeneralRe: SQL Server Access rights Pin
Nick Seng11-Mar-04 14:18
Nick Seng11-Mar-04 14:18 
GeneralSQL code and datagrid Pin
ASGill10-Mar-04 13:24
ASGill10-Mar-04 13:24 
GeneralRe: SQL code and datagrid Pin
MasudM10-Mar-04 22:23
MasudM10-Mar-04 22:23 
GeneralRe: SQL code and datagrid Pin
ASGill11-Mar-04 2:04
ASGill11-Mar-04 2:04 
GeneralRe: SQL code and datagrid Pin
MasudM11-Mar-04 5:14
MasudM11-Mar-04 5:14 
GeneralRe: SQL code and datagrid Pin
Mike Dimmick11-Mar-04 2:56
Mike Dimmick11-Mar-04 2:56 
Sounds like you need to JOIN your tables together.

If you want only rows that appear in both tables, use an INNER JOIN. If you want all rows that appear on the left-hand side of the JOIN, with any data appearing on the right-hand side if available, use a LEFT JOIN. Vice-versa, use a RIGHT JOIN. If you want all rows from both tables, regardless of whether corresponding data appears on the other side, use a FULL OUTER JOIN. Finally, if you don't want to use a join condition (the ON clause) and want to output every row of your left-hand table joined to every row of your right-hand table, use a CROSS JOIN.

Example: you have tables OrderLine and Product. An OrderLine maps between an order and the products on that order, and includes the quantity ordered. You want to print an invoice, listing the product details and total prices. You might write something like:
SELECT
  OrderLine.Sequence,
  OrderLine.Quantity,
  Product.ShortDesc,
  Product.Price AS ItemPrice,
  OrderLine.Quantity * Product.Price AS LinePrice
FROM
  OrderLine INNER JOIN Product
    ON OrderLine.ProductID = Product.ProductID
If, somehow, we had OrderLine rows with ProductID not in the Product table, and we wanted to show those anyway, we would use a LEFT JOIN in the above query. Any fields we refer to from Product, for OrderLine rows with no matching ProductID, will be NULL (e.g. ShortDesc and Price will be NULL for the above query). The INNER JOIN omits these rows.

Note that if there is more than one row on each side that matches the join condition, all those rows will be output, joined to each other. For the sake of argument, let's say that we have two OrderLine rows which refer to ProductID 10, and there are two rows in Product where ProductID is 10 (you would try to avoid this in an ordering system!). Let's give them descriptions ProductA and ProductB. You might get the result
Sequence Quantity ShortDesc ItemPrice LinePrice
-----------------------------------------------
    1        2    ProductA      5        10
    1        2    ProductB      6        12
    2        1    ProductA      5         5
    2        1    ProductB      6         6


Stability. What an interesting concept. -- Chris Maunder
GeneralRe: SQL code and datagrid Pin
ASGill11-Mar-04 15:39
ASGill11-Mar-04 15:39 
GeneralDataView from DataTable Pin
Le centriste10-Mar-04 10:33
Le centriste10-Mar-04 10:33 
GeneralADO Disconnected Recordset Pin
cmacgowan10-Mar-04 6:41
cmacgowan10-Mar-04 6:41 
GeneralAbout CrystalReports ... Pin
DustInTheWind9-Mar-04 10:18
DustInTheWind9-Mar-04 10:18 
GeneralRe: About CrystalReports ... Pin
jscorales11-Mar-04 22:52
jscorales11-Mar-04 22:52 
GeneralI need suggestions - datatabase structure Pin
zzaa9-Mar-04 4:26
zzaa9-Mar-04 4:26 
GeneralRe: I need suggestions - datatabase structure Pin
Andy Harman9-Mar-04 10:17
Andy Harman9-Mar-04 10:17 
QuestionHow to create an NTEXT from string data? Pin
kumaichi9-Mar-04 3:22
kumaichi9-Mar-04 3:22 
GeneralRunning SQL script in .NET GUI Pin
xsigroup9-Mar-04 3:21
xsigroup9-Mar-04 3:21 
GeneralProblems with CREATE DATABASE Pin
Frederick S Jones9-Mar-04 3:12
Frederick S Jones9-Mar-04 3:12 
GeneralRe: Problems with CREATE DATABASE Pin
Colin Angus Mackay9-Mar-04 5:39
Colin Angus Mackay9-Mar-04 5:39 
GeneralRe: Problems with CREATE DATABASE Pin
Frederick S Jones11-Mar-04 2:08
Frederick S Jones11-Mar-04 2:08 
GeneralGenerating SQL script Pin
Asad Hussain9-Mar-04 2:51
Asad Hussain9-Mar-04 2:51 
GeneralRe: Generating SQL script Pin
Colin Angus Mackay9-Mar-04 5:34
Colin Angus Mackay9-Mar-04 5:34 
QuestionOracle Notification Services?? Pin
Chen Venkataraman8-Mar-04 9:43
Chen Venkataraman8-Mar-04 9:43 
GeneralFind right Sql Collation Problem !! Pin
MasudM8-Mar-04 5:49
MasudM8-Mar-04 5:49 
GeneralHi, everyone. I need help about SQL 2000 server. Pin
jlizardo8-Mar-04 4:01
jlizardo8-Mar-04 4:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.