Backjoin na sebe sama (tabulka s aliasem)

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
Tomáš Jacík
Člen | 147
+
0
-

Mám tabulku actuarial:

CREATE TABLE `actuarial` (
  `actuarial_id` bigint(12) unsigned NOT NULL COMMENT 'cislo dokladu',
  `proforma_id` bigint(12) unsigned DEFAULT NULL COMMENT 'cislo proforma faktury',
  `aoc_actuarial_id` bigint(12) unsigned DEFAULT NULL COMMENT 'cislo dobropisu',
  PRIMARY KEY (`actuarial_id`),
  KEY `i_aoc_actuarial_id` (`aoc_actuarial_id`)
);

Kde aoc_actuarial_id je číslo faktury, která je dokladem dobropisována.

Mám následující podmínku:

$connection->where("aoc(actuarial).aoc_actuarial_id IS NULL OR ABS(aoc.price) < actuarial.price");

Která tu tabulku ovšem joinuje přesně opačně než bych potřeboval:

LEFT JOIN `actuarial` AS `aoc` ON `actuarial`.`aoc_actuarial_id` = `aoc`.`actuarial_id

Potřebuji toto:

LEFT JOIN `actuarial` AS `aoc` ON `actuarial`.`actuarial_id` = `aoc`.`aoc_actuarial_id

Bohužel se mi nedaří tu podmínku obrátit. Zkoušel jsem cpát dvojtečku všude možně, ale nedělá to co má.

norbe
Backer | 408
+
0
-

Jestli jsem správně pochopil co potřebuješ tak by to mělo být takhle:

$connection->where(":actuarial(aoc_actuarial_id).actuarial_id IS NULL OR ABS(:actuarial(aoc_actuarial_id).price) < actuarial.price");

ale jelikož se té tabulce nenastaví alias, tak to hodí chybu. Takže buď je to bug a nebo už je pozdě a nepřemýšlí mi to :-)

Co na to @hrach ?

Tomáš Jacík
Člen | 147
+
0
-

Správně, takto se tam nenastaví alias. Např. tohle mi funguje správně:

$connection->where(":actuarial_pay.pay_id IS NOT NULL");

Tam ale není ten alias. A alias samotný jede taky, ale ne s tím backjoin dokupy nebo nevím jak to správně zapsat, z regexpu v kódu jsem to nebyl schopen vyčíst.

norbe
Backer | 408
+
0
-

Zkus popsat slovy co má být přesně výsledkem a co s tím chceš dělat. Určitě to dáme nějak dohromady, ještě jsem nenarazil na nic, co by ndb nezvládlo nějakým způsobem vytáhnout…

Jan Suchánek
Člen | 404
+
0
-

Podle mě s chce připojit na stejnou tabulku aliasem a to v NDB nejde, aspoň myslim že ne.

SELECT `actuarial`.*
FROM `actuarial`
LEFT JOIN `actuarial` AS `aoc`
ON `actuarial`.`aoc_actuarial_id` = `aoc`.`actuarial_id

Samozřejmě pokud by chtěl vybrat jen acturial podle id tak to bude sranda.

$ids = $context->table("actuarial")->where("aoc_actuarial_id",$id);
$rows = $context->table("actuarial")->where("actuarial_id", $ids);

Tzn jedině view?

A nebo?

$ids = $dbContext->getConnection()->query("
	SELECT a1.* FROM acturial a1
	JOIN acturial a2 ON a1.id = a2.aoc_actuarial_id AND ABS(a2.price) < a1.price
")->fetchPairs("id","id");

// A teď získat ActiveRow
$acturials = $dbContext->table("acturial")->where("ids", $ids);

Editoval jenicek (10. 7. 2014 13:16)