/    Sign up×
Community /Pin to ProfileBookmark

searching two tables with the same field names

Morning All!

Simple one for experts like you, but confusing for me.

I have two tables which from which I need to search and extract data at the same time, in the same query. However, the tables share the same field names.

Can a kindly soul show me how my select satement should read, and how I can subsequently extract the data?

The first table is directory_editing. The second table is directory_published.

The field names in both tables are:
– directory_id
– directory_style_id
– directory_name
– time
– e_user_id

Thanks!

Simon

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@aussie_girlJul 09.2006 — What about

SELECT * FROM directory_editing, directory_published

WHERE directory_editing.columnname = directory_published.columnname
Copy linkTweet thisAlerts:
@SFADuncanauthorJul 09.2006 — got that.... but my problem is extracting the data when both tables contain fields with the same names.

eg I'm trying to to the following but I don't know which field is being picked up:

$directories = fetch_all_directories();

if (is_array($pub_directories))

{

foreach ($directories as $row)

{

$edit_dir_id = $row['directory_id'];

$edit_dir_style_id = $row['directory_style_id'];

$edit_dir_name = $row['directory_name'];

$edit_time = $row['time'];

$edit_e_user_id = $row['e_user_id'];

$pub_dir_id = $row['directory_id'];

$pub_dir_style_id = $row['directory_style_id'];

$pub_dir_name = $row['directory_name'];

$pub_time = $row['time'];

$pub_e_user_id = $row['e_user_id'];

}

}[/QUOTE]
Copy linkTweet thisAlerts:
@aussie_girlJul 09.2006 — ewww...that's not gonna work, you are just overwriting the variables, you may to do two queries...
Copy linkTweet thisAlerts:
@SFADuncanauthorJul 09.2006 — yeah.... maybe.... I thought there was a way of joining..... but you could be right!
Copy linkTweet thisAlerts:
@NogDogJul 09.2006 — You'll have to query the fields explicitly:
<i>
</i>SELECT e.directory_id as e_directory_id,
e.directory_style_id as e_directory_style_id,
e.directory_name as e_directory_name,
e.time as e_time,
e.e_user_id as e_e_user_id,
p.directory_id as p_directory_id,
p.directory_style_id as p_directory_style_id,
p.directory_name as p_directory_name,
p.time as p_time,
p.e_user_id as p_e_user_id
FROM directory_editing e, directory_published p
WHERE &lt;whatever where clause you want to use&gt;

Now your mysql_fetch array keys will use the names following the "as" keywords in the query above.
×

Success!

Help @SFADuncan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...