/    Sign up×
Community /Pin to ProfileBookmark

Selecting from 2 unrelated tables with a condition

Hi,
Is it possible to select from 2 unrelated tables using one statement?

I have 2 tables, and I want to pull from both of them ordered on date, i.e. The first table is for events, the second is for holidays, I want to pull date from both of them but ordered by date, can this be done?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@GUIRDec 11.2008 — Hi,

I think you are saying about a couple of table those have no relationship.

If so, I think following should work, others, pls correct me if I am wrong.

Say, table events has following fields

evtId|evtName|evtDate|evtVenue

Say, table holidays have following

holId|holReason|holDate

Now you have two different tables also with different column count.

Then,

[CODE]SELECT evtId as id, evtName as description, evtDate as oDate, evtVenue as venue FROM events
UNION
SELECT holId as id, holReason as description, holDate as oDate, 'dummy' as venue FROM holiday
ORDER BY 2[/CODE]


'dummy' value for keeping column count match

ORDER BY 2, is for sorting resultset index 2, that is date.

If I am wrong pls let me know someone ?

Best Regards
×

Success!

Help @livewire1974 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.15,
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,
)...