Frage

Wie kann ich etwas tun, aber den Fehler column common_reference is ambiguous vermeiden? Ich weiß, es ist nicht eindeutig, ich will es alle Ergebnisse für common_reference von Table_One auszuwählen, und Table_Two das gleiche.

SELECT * FROM table_one, table_two WHERE common_reference = 42

Dies ist offensichtlich nicht zur Arbeit gehen, so wie verwende ich Unterabfragen zu erreichen, was ich brauche?

War es hilfreich?

Lösung 4

As suggested by Mchl in the comments UNION was the solution.

Andere Tipps

Start with...

SELECT * FROM table_one, table_two WHERE table_one.common_reference = 42

...and continue this road:

SELECT * FROM table_one as T1, table_two as T2 WHERE T1.common_reference = 42
SELECT table_one.* FROM table_one, table_two WHERE table_one.common_reference = 42

MySQL will let you know via error if there is an unresolvable ambiguity in your SQL.

If you want to reference a specific field which also exists in another table (or the same table if you're doing a join on itself), use fully qualified fields

SELECT * FROM table_one, table_two WHERE table_one.common_reference = 42

and/or table alias's

SELECT * FROM table_one T1a, table_one T1b WHERE T1a.common_reference = 42

NB: Fair warning, as Mchl noted, these are Cartesian products and not typical joins.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top