/    Sign up×
Community /Pin to ProfileBookmark

mySQLquery question

Guys –

I’m doing this:

[code=php]
$query = ‘SELECT * FROM sales WHERE invnum =’.$invnum.’ AND relcid =’.$cid;
$result = mysql_query($query);
$invoice = mysql_fetch_assoc($result);
$query = ‘SELECT * FROM cdata WHERE cid =’.$cnum;
$result = mysql_query($query);
$customer = mysql_fetch_assoc($result);
[/code]

Now, as you see I’m doing 2 querys to the DB to different tables. I’m pretty damn sure this is what JOIN is all about, but I’m a newbie and horribly scared to even try it!

Anyone want to explain why and how I could/should use a JOIN? be detailed please… my old brain is working slow this morning.

John

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2004 — A JOIN is typically used on 2 tables whre they have a common field/column

http://www.w3schools.com/sql/sql_join.asp

I search on google for

SQL JOIN will produce even better results.
Copy linkTweet thisAlerts:
@NedalsMar 07.2004 — The link Khalid Ali gave you explains things quite well but, in practice, there are a few tricks available when using mySQL. As Khalid Ali mentioned JOINs are used when dealing with 2 or more related tables. In your case the tables are independant and will use seperate querys.

RE: the link

You seldom, if ever, will need INNER JOIN, do this instead..

'SELECT s.invnum, c.custname FROM sales s, customer c WHERE s.invnum=$invnum AND c.custid=s.cid';

(Note: In perl you don't need to concat the variable. This may not be true for PHP)

Some tricks here:

1. Don't use '*'. As you expand your database, you may decide to add an extra column. When you do, all your '*' queries will need to be edited.

2. 'sales s' is a shorthand way of writing 'sales AS s'. You can now use the 's' to identify which table the column belongs to. You may have read that, as long as the column names are unique, you don't need to do this BUT do it anyway. It's a good habit to get into.

LEFT JOIN is the only one you really need. A RIGHT JOIN can be coverted to a LEFT join by swapping the joined table with the FROM table.

'SELECT s.invnum, c.custname, v.via

FROM sales s, customer c, v.via

WHERE s.invnum=$invnum AND c.custid=s.cid AND v.invnum=s.invnum';

Suppose you had this and 'via' was NULL for a couple of items in your sales table, you would get no result for those invnums

to solve this, the LEFT JOIN is used

'SELECT s.invnum, c.custname, v.via

FROM sales s, customer c

LEFT JOIN via v ON v.invnum=s.invnum

WHERE s.invnum=$invnum AND c.custid=s.cid AND';

Now all the invnums will be returned along with blank fields for those NULL 'via's

Hope that helps a little.
×

Success!

Help @Bigjohn 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.18,
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,
)...