Ticket #775 (closed Patch: duplicate)

Opened 3 months ago

Last modified 5 weeks ago

Joining Multiple Tables with Postgres Driver

Reported by: Jeffrey04 Owned by: zombor
Priority: major Milestone: 2.2.1
Component: Libraries:Database Version: 2.2 Release
Keywords: Cc:

Description

I don't have a fix for this at the moment. While joining 2 tables is fixed in #774, but when I am working on joining 3 tables, i get a query like this.

SELECT "v"."value"
FROM users u JOIN "profile_values v", "profile_fields f" ON (u.uid = v.uid) AND (f.fid = v.fid)
WHERE "uid" = '1'
    AND "profile_organization" = 'organization'

As far as i know, postgres (8.3) doesn't work this way. A proper join statement should look something like

SELECT f.name, v.value
FROM users u 
    JOIN 
        ("profile_values" v JOIN "profile_fields" f ON f.fid = v.fid)
    ON v.uid = u.uid
WHERE u.uid = 1;

Change History

Changed 3 months ago by Jeffrey04

  • type changed from Bug to Patch

quick fix

			$sql .= ' '.$database['join']['type'].'JOIN '.implode(', ', $database['join']['tables']).' ON '.implode(' AND ', array_reverse($database['join']['conditions'], TRUE));

Changed 3 months ago by Jeffrey04

  • type changed from Patch to Bug

sorry.. the above patch didn't work

Changed 7 weeks ago by postlogic

  • type changed from Bug to Patch

Working quick fix

$sql .= ' '.$databasejoin?type?.'JOIN ';

if (count($databasejoin?tables?) > 1) {

$sql .= '('.implode(', ', $databasejoin?tables?).') ON '.implode(' AND ', $databasejoin?conditions?);

} else {

$sql .= implode(', ', $databasejoin?tables?).' ON '.implode(' AND ', $databasejoin?conditions?);

}

Changed 7 weeks ago by postlogic

Err, proper formatting..

			$sql .= ' '.$database['join']['type'].'JOIN ';
			
			if (count($database['join']['tables']) > 1) {
				$sql .= '('.implode(', ', $database['join']['tables']).') ON '.implode(' AND ', $database['join']['conditions']);
			} else {
				$sql .= implode(', ', $database['join']['tables']).' ON '.implode(' AND ', $database['join']['conditions']);
			} 

Changed 5 weeks ago by Shadowhand

  • status changed from new to closed
  • resolution set to duplicate

Duplicate of #779.

Note: See TracTickets for help on using tickets.