P. 1
MYSQL & PHP Referance

MYSQL & PHP Referance

|Views: 12|Likes:
Published by jwzumwalt
A simple guide and reference for using MySql with PHP. Includes many simple examples. Most topics include a short 5 to 10 line program that demonstrates a concept.
A simple guide and reference for using MySql with PHP. Includes many simple examples. Most topics include a short 5 to 10 line program that demonstrates a concept.

More info:

Published by: jwzumwalt on Feb 07, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

11/21/2015

pdf

text

original

This JOIN combines both tables (as if a LEFT and RIGHT JOIN where
used together). MySql does not provide a FULL JOIN because it is rarely
needed. You can create one by using a union statement.

A “STRAIGHT” JOIN is a full JOIN except that the JOIN occurs in the
order given in the command line. Otherwise, the database engine is free
to display things in the order in which is most convenient.

SELECT t1.*, t2.* FROM t1,t2 WHERE t1.cust_id = t2.cust_num

or

SELECT * FROM t1
LEFT JOIN t2
ON t1.cust_id = t2.cust_num

UNION
SELECT * FROM t1
RIGHT JOIN t2
ON t1.cust_id = t2.cust_num

Table “t1”

Table “t2”

cust_id

credit

cust_num

phone

1

50

3

123-4567

2

75

4

234-5678

3

100

5

345-6789

4

150

6

456-7890

Result:

cust_id credit cust_num

phone

1

50.00

NULL NULL

2

75.00

NULL NULL

3 100.00

3 123-4567

4 150.00

4 234-5678

NULL

NULL

5 345-6789

NULL

NULL

6 456-7890

To do this with three tables…

-- three tables t1, t2, t3:

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 ON t2.id = t3.id

UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 ON t2.id = t3.id

UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
RIGHT JOIN t3 ON t2.id = t3.id

MySql Reference – by Jan Zumwalt NeatInfo.com – February, 06, 2012
Pg - 33 of 43

UNION: returns a table with column positions merged to the first table’s field names.

SELECT * FROM t1

UNION
SELECT * FROM t2

Table “t1”

Table “t2”

cust_id

credit

cust_num

phone

1

50

3

123-4567

2

75

4

234-5678

3

100

5

345-6789

4

150

6

456-7890

Result:

cust_id

credit

1 50.00

2 75.00

3 100.00

4 150.00

3 123-4567

4 234-5678

5 345-6789

6 456-7890

MySql Reference – by Jan Zumwalt NeatInfo.com – February, 06, 2012
Pg - 34 of 43

GROUP: returns a table with unique row data.

Suppose we would like to know what types of credit we offer our customers.

SELECT credit FROM t1 GROUP BY credit

Table “t1”

cust_id

credit

1

75

2

75

3

100

4

100

5

150

Result:

credit

75.00

100.00

150.00

MySql Reference – by Jan Zumwalt NeatInfo.com – February, 06, 2012
Pg - 35 of 43

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->