join, union etc - confused - Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more
Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Webmaster Tech > Databases - MySQL, SQL, Oracle, Access and others

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-28-2011, 06:15 PM   #1 (permalink)
Junior Member
 
Join Date: Sep 2007
Posts: 4
Thumbs up join, union etc - confused (solved)

so confused with not knowing exactly which to use - seen loads of qs on this but im sure it must be simple

table 1 consists of

id | cat | name
1 | 1 | notinterested
2 | 2 | name a
3 | 2 | name b
4 | 2 | name c
5 | 3 | notinterested

2nd table

id | catid | url
1 | 2 | a.jpg (name a)
2 | 2 | b.jpg (name a)
3 | 2 | c.jpg (name a)
4 | 3 | d.jpg (name b)
5 | 3 | e.jpg (name b)
6 | 3 | f.jpg (name b)
7 | 4 | g.jpg (name c)
8 | 4 | h.jpg (name c)
9 | 4 | i.jpg (name c)


the catid in 2nd table is based on the original id from the first table

so basically what i need to do is select everything from table one that has cat=2 and display that name next to its 1st corresponding url from table 2

so the echo result would be

name a - a.jpg
name b - d.jpg
name c - g.jpg


if this makes any sense whatsoever

solved

SELECT table1.cat, table1.id, table2.id, table2.url, table2.catid FROM table1 LEFT OUTER JOIN table2 ON table2.catid = table1.id WHERE table1.cat = '2' GROUP BY table2.catid

Last edited by adeibiza; 03-29-2011 at 08:23 AM. Reason: solved
adeibiza is offline   Reply With Quote
Sponsored Links
Old 03-30-2011, 02:56 AM   #2 (permalink)
Regular User
 
Join Date: Mar 2011
Posts: 102
Default Re: join, union etc - confused

select* from table name where cat="value"
ashusood is offline   Reply With Quote
Old 04-08-2011, 03:44 AM   #3 (permalink)
Regular User
 
Join Date: Apr 2011
Posts: 115
Default Re: join, union etc - confused

use innerjoin
chunnilal is offline   Reply With Quote
Old 05-02-2011, 06:46 AM   #4 (permalink)
Member
 
Join Date: Apr 2011
Posts: 136
Default Re: join, union etc - confused

Not working dear.

Canada Drugs
Canada Drugs
Buy Orthovisc
sweetsianjali is offline   Reply With Quote
Old 05-02-2011, 06:47 AM   #5 (permalink)
Member
 
Join Date: Apr 2011
Posts: 136
Default Re: join, union etc - confused

Wonderful article,thanks for putting this together! “This is obviously one great post. Thanks for the valuable information and insights you have so provided here. Keep it up!”

Buy Perlane
Buy Plavix online
Buy Perlane
sweetsianjali is offline   Reply With Quote
Old 05-02-2011, 06:49 AM   #6 (permalink)
Member
 
Join Date: Apr 2011
Posts: 136
Default Re: join, union etc - confused

Very well written text, thanks to the site admin for the good work.


Diesel Card
Euflexxa
Buy Xenical Online
sweetsianjali is offline   Reply With Quote
Old 05-03-2011, 08:20 AM   #7 (permalink)
Junior Member
 
Join Date: Jun 2010
Location: Houston
Posts: 26
Default Re: join, union etc - confused

I am not sure how a UNION would help.

Code:
SELECT firsttable.name, secondtable.url
FROM secondtable
INNER JOIN firsttable ON secondtable.catid = firsttable.id
WHERE firsttable.cat = 2
ap2010 is offline   Reply With Quote
Old 05-13-2011, 04:56 AM   #8 (permalink)
Member
 
Join Date: Apr 2011
Posts: 136
Default Re: join, union etc - confused

Thank u very much.

Cheap London Theatre Ticket
sweetsianjali is offline   Reply With Quote
Old 06-28-2011, 08:21 AM   #9 (permalink)
Junior Member
 
Join Date: Jun 2011
Posts: 57
Default Re: join, union etc - confused

Hey hi...i just read your post about Join , unjoin. it's really good one. i was just trying to fine some solution. i think some of the user is right. i will surely implement it and revert back for the good result.
Thank you for such a nice Information.

MBA | MBA Colleges in Mumbai
rahaul is offline   Reply With Quote
Old 09-16-2011, 04:34 PM   #10 (permalink)
Junior Member
 
Join Date: Sep 2011
Location: Long Islanf, NY, USA
Posts: 33
Default Re: join, union etc - confused

For anyone who stumbles across this thread and needs help with unions, they're just different selects (they can be from different tables or different views of the same table). All corresponding fields MUST be of the same type (or cast to the same type if the database you're on allows casting). IOW

SELECT ship_date, price, description from table
UNION SELECT received_date, cost, name from table2

would work. You'd get one row for each row in table and one row for each row in table2. Each member of the union can have its own WHERE clause too.

UNION will not include data that's duplicated between tables. If you want it all returned (sort of a non-distinct UNION) use UNION ALL.

(I have one situation in which paid bills get moved to a different table. If I need to find the bill, paid or not, I do a union with unpaid and paid, and get a row returned regardless of which table the data is in at the time.)
Rukbat is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 08:05 AM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2
Webmaster Forums
Web Hosting | Chicago Web Hosting | Web Hosting