473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Fetching record from other table if exist

Hello,

I have 2 tables, one contains log of all incoming phone calls, and the
other one is simply a phone book.
I want to fetch caller's name if it's already exist in the phone book,
and if it doesn't exist yet just fetch caller's phone number.
Is it possible to do this in just a single query?
Something like:

SELECT Switch((SELECT count(a.*) FROM phone_log a, phone_book b WHERE
a.phone_number = b.phone_number) <0, b.member_name, (SELECT
count(a.*) FROM phone_log a, phone_book b WHERE a.phone_number =
b.phone_number) = 0, a.phone_number) FROM phone_log a, phone_book b

Thanks!

Oct 25 '07 #1
5 2263
On Oct 24, 11:56 pm, Stephen <win32orp...@gmail.comwrote:
Hello,

I have 2 tables, one contains log of all incoming phone calls, and the
other one is simply a phone book.
I want to fetch caller's name if it's already exist in the phone book,
and if it doesn't exist yet just fetch caller's phone number.
Is it possible to do this in just a single query?
Something like:

SELECT Switch((SELECT count(a.*) FROM phone_log a, phone_book b WHERE
a.phone_number = b.phone_number) <0, b.member_name, (SELECT
count(a.*) FROM phone_log a, phone_book b WHERE a.phone_number =
b.phone_number) = 0, a.phone_number) FROM phone_log a, phone_book b

Thanks!
It sounds like you need a LEFT JOIN.
sSQL="SELECT a.phone_number, b.member_name FROM a LEFT JOIN b ON
a.phone_number=b.phone_number WHERE a.phone_number = ' " &
sPhoneNumber & " ' ;

Oct 25 '07 #2
On Oct 25, 9:07 pm, OldPro <rrossk...@sbcglobal.netwrote:
On Oct 24, 11:56 pm, Stephen <win32orp...@gmail.comwrote:
Hello,
I have 2 tables, one contains log of all incoming phone calls, and the
other one is simply a phone book.
I want to fetch caller's name if it's already exist in the phone book,
and if it doesn't exist yet just fetch caller's phone number.
Is it possible to do this in just a single query?
Something like:
SELECT Switch((SELECT count(a.*) FROM phone_log a, phone_book b WHERE
a.phone_number = b.phone_number) <0, b.member_name, (SELECT
count(a.*) FROM phone_log a, phone_book b WHERE a.phone_number =
b.phone_number) = 0, a.phone_number) FROM phone_log a, phone_book b
Thanks!

It sounds like you need a LEFT JOIN.
sSQL="SELECT a.phone_number, b.member_name FROM a LEFT JOIN b ON
a.phone_number=b.phone_number WHERE a.phone_number = ' " &
sPhoneNumber & " ' ;
Hello,
Thanks for the help.

I'm afraid that's not the solution to my problem, because that would
mean i have to pass variable to the query.
While what i really want is simply making a check from table phone
book.
If there's already a record in that table that contains caller's phone
number use member name, otherwise use the
phone number.
Oct 25 '07 #3
On Oct 25, 9:38 am, Stephen <win32orp...@gmail.comwrote:
On Oct 25, 9:07 pm, OldPro <rrossk...@sbcglobal.netwrote:


On Oct 24, 11:56 pm, Stephen <win32orp...@gmail.comwrote:
Hello,
I have 2 tables, one contains log of all incoming phone calls, and the
other one is simply a phone book.
I want to fetch caller's name if it's already exist in the phone book,
and if it doesn't exist yet just fetch caller's phone number.
Is it possible to do this in just a single query?
Something like:
SELECT Switch((SELECT count(a.*) FROM phone_log a, phone_book b WHERE
a.phone_number = b.phone_number) <0, b.member_name, (SELECT
count(a.*) FROM phone_log a, phone_book b WHERE a.phone_number =
b.phone_number) = 0, a.phone_number) FROM phone_log a, phone_book b
Thanks!
It sounds like you need a LEFT JOIN.
sSQL="SELECT a.phone_number, b.member_name FROM a LEFT JOIN b ON
a.phone_number=b.phone_number WHERE a.phone_number = ' " &
sPhoneNumber & " ' ;

Hello,
Thanks for the help.

I'm afraid that's not the solution to my problem, because that would
mean i have to pass variable to the query.
While what i really want is simply making a check from table phone
book.
If there's already a record in that table that contains caller's phone
number use member name, otherwise use the
phone number.- Hide quoted text -

- Show quoted text -
How do you know the caller's phone number without a variable? If you
are just looking for a report of all callers and their matching names,
then modify the query slightly:
sSQL="SELECT a.phone_number, b.member_name FROM a LEFT JOIN b ON
a.phone_number=b.phone_number"

Oct 25 '07 #4
On Oct 25, 11:50 pm, OldPro <rrossk...@sbcglobal.netwrote:
On Oct 25, 9:38 am, Stephen <win32orp...@gmail.comwrote:
On Oct 25, 9:07 pm, OldPro <rrossk...@sbcglobal.netwrote:
On Oct 24, 11:56 pm, Stephen <win32orp...@gmail.comwrote:
Hello,
I have 2 tables, one contains log of all incoming phone calls, and the
other one is simply a phone book.
I want to fetch caller's name if it's already exist in the phone book,
and if it doesn't exist yet just fetch caller's phone number.
Is it possible to do this in just a single query?
Something like:
SELECT Switch((SELECT count(a.*) FROM phone_log a, phone_book b WHERE
a.phone_number = b.phone_number) <0, b.member_name, (SELECT
count(a.*) FROM phone_log a, phone_book b WHERE a.phone_number =
b.phone_number) = 0, a.phone_number) FROM phone_log a, phone_book b
Thanks!
It sounds like you need a LEFT JOIN.
sSQL="SELECT a.phone_number, b.member_name FROM a LEFT JOIN b ON
a.phone_number=b.phone_number WHERE a.phone_number = ' " &
sPhoneNumber & " ' ;
Hello,
Thanks for the help.
I'm afraid that's not the solution to my problem, because that would
mean i have to pass variable to the query.
While what i really want is simply making a check from table phone
book.
If there's already a record in that table that contains caller's phone
number use member name, otherwise use the
phone number.- Hide quoted text -
- Show quoted text -

How do you know the caller's phone number without a variable? If you
are just looking for a report of all callers and their matching names,
then modify the query slightly:
sSQL="SELECT a.phone_number, b.member_name FROM a LEFT JOIN b ON
a.phone_number=b.phone_number"
Hello,

I get the caller's phone number from the phone_log table.
Yes, it's like matching names, but when i found no record (caller's
phone number) on phone book table simply return caller's phone number
in the phone_log table.
With your query, i believe would return NULL record if there's no
matching phone number and that's not what i want.

Thanks

Oct 25 '07 #5
Hello,

I modified your last query, and it works!
Here's the working query:

SELECT IIF(b.member_name IS NULL, a.phone_number, b.member_name) FROM
phone_log a LEFT JOIN phone_book b ON a.phone_number = b.phone_number

Thanks for your help, OldPro!

Oct 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kenneth Osenbroch | last post by:
Hi. I want to create a trigger that only allows delete from table A if corresponding record in table B does not exist. Any idea on how this can be done? Thanks, Kenneth.
8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
0
by: Shujun Huang | last post by:
Hi, I am working on converting Informix database to Postgre. I have one question for fetching records using PostgreSQL. The record I am fetching is a variable size text string. Before fetching...
7
by: Stephen Poley | last post by:
I have the following situation: - a table of employees, keyed on employee-id; - a table of training sessions, keyed on session-id; - a requirement to log who was present at which session, plus...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
4
by: is49460 | last post by:
Any help would be very appreciated!!! I have two tables: Main Data and Archived Data. Each table has the same fields (about 50 or so fields). Main data contains records that are updated every...
1
by: vbDavidC | last post by:
I am adding a new record to a table via a dataset/adapter. I have got the following to work for me but I am wondering if there is a better way to do this. I am having to have something in my...
0
debasisdas
by: debasisdas | last post by:
We can fetch from a cursor into one or more collections: DECLARE TYPE NameList IS TABLE OF employees.last_name%TYPE; TYPE SalList IS TABLE OF employees.salary%TYPE; CURSOR c1 IS SELECT...
4
by: PW | last post by:
Hi, I set up a relationship between two tables with the itineraryid fields in both tables: tblDailyItinerary tblDailyMeals I have a form that writes a record to tblDailyItinerary that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.