Web Hosting Talk







View Full Version : MySQL Query Dilema


portal
07-19-2006, 04:42 AM
$query = mysql_query("SELECT
doors.oid AS doid,
windows.oid AS oid,
doors.height AS dheight,
windows.height AS height,
doors.width AS dwidth,
windows.width AS width,
doors.price AS dprice,
windows.price AS price,
windows.ocid AS ocid,
windows.grills AS grills,
windows.iid AS iid,
windows.lid AS lid,
windows.sid AS sid,
doors.doortype AS doortype,
doors.doorstyle AS doorstyle,
doors.category AS category,
doors.doormat AS doormat,
doors.concol AS concol,
doors.success AS dsuccess,
doors.type AS type,
doors.uid AS duid
FROM doors, windows WHERE
windows.success='0' AND doors.success='0'
AND windows.uid='$uid' AND doors.uid='$uid' ORDER BY windows.sid ASC, windows.oid ASC, doors.oid $max");


This throws a cartisian pattern in the result, meaning that if I have 3 entries in table "windows" and 1 entry in table "doors" the results will show:

Window 1
Door 1
Window 2
Door 1
Window 3
Door 1

What I need this query to accomplish is the following:

Window1
Window 2
Window 3
Door 1

meaning that I only need it to show each entry only once, instead of repeating that entry multiple times.

This is complex, hopefully somebody can help me.

maxymizer
07-19-2006, 08:18 AM
Do "windows" and "doors" have anything in common, e.g. - does anything in the database assign a window to a door (or vice versa) or are they just stimply 2 separate tables with no connection whatsoever?

portal
07-19-2006, 08:22 AM
they are two seperate tables with no connection, the reason i wanted to JOIN them was because it would make life easier using pagination if it were in one query.

maxymizer
07-19-2006, 08:44 AM
If you want to have results of select from 2 (or more tables) which have no connection whatsoever, you use UNION. Also, UNION requires that both tables have the EXACT same number of returned rows.

The query with UNION would go like this:

SELECT
window.id
FROM
windows
WHERE [where conditions]
UNION
SELECT
door.id
[etc.]