Solved

SQL Select Statement

Posted on 2011-02-25
1
385 Views
Last Modified: 2012-06-27
I am attempting to create a select statement that will pair data with the correct record.  I have an ACCIDENT Table which contains the Accident Reports.  I then have a MASTER Table that holds the units involved in the accident.  The third table is the VEHICLE table which will hold all of the codes related to the Unit Number that was in the ACCIDENT.  

The Key in the ACCIDENT table is the report number - OCA_T

The Master Table is using SELECT * FROM MASTER  WHERE OCA_T = ?CURRENTOCA and relation == "DRIVER"   -  (This is working fine)  In the Master Table the there is a field labeled NO which contains the Unit Number.

In my VEHICLE table, I have OCA_T and NO fields.  I want to have it pair up with the MASTER table if the OCA_T+NO == the Master.OCA_T+MASTER.NO.

I am using the following and it does not work....  SELECT * FROM VEHICLE where OCA_T = ?CURRENTOCA .AND. NO = MASTER.NO order by accident  (the accident order is a simple OCA_T+NO)

Any help is greatly appreciated!!

I am using the Select statement as it works with my my Visual Fox Tables as well as SQL.
0
Comment
Question by:goautl
1 Comment
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
ID: 34984280
Your command
SELECT * FROM VEHICLE where OCA_T = ?CURRENTOCA .AND. NO = MASTER.NO
will select VEHICLE records for one MASTER.NO value (the one which is in the current record).

If you need to retrieve data from two or more tables in one query then you have to include all these tables in the SELECT command and define the joining condition. The simplest command would look like this one:

SELECT * FROM VEHICLE, MASTER ;   && list of output columns (* means all columns) and input tables
where VEHICLE.OCA_T = MASTER.OCA_T ;  && first part of joining condition
  AND VEHICLE.NO = MASTER.NO ;   && second part of joining condition
  AND VEHICLE.OCA_T = ?CURRENTOCA  && filter condition

or same command:

SELECT * FROM VEHICLE ;
INNER JOIN MASTER ON VEHICLE.OCA_T = MASTER.OCA_T  AND VEHICLE.NO = MASTER.NO ;
where VEHICLE.OCA_T = ?CURRENTOCA

But this explanation probably does not help to understand the queries, so I would recommend some SQL course or tutorial, e.g. http://www.w3schools.com/sql/default.asp or  http://www.sqlcourse.com/index.html
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now