mysql error 1066

2019-09-23 10:47发布

问题:

$id=$_GET["id"];
$query= "
SELECT
blomster_produkter.blomster_produkt_id,
blomster_produkter.blomster_produkt_navn,
blomster_produkter.blomster_produkt_pris
FROM
blomster_produkter
INNER JOIN blomster_produkter ON 
blomster_produkter.FK_blomster_produkt_id=blomster_produkter.blomster_produkt_navn     
blomster_produkter.FK_blomster_produkt_id=blomster_produkter.blomster_produkt_pris
blomster_produkter.FK_blomster_produkt_id=blomster_produkter.blomster_produkt_id
WHERE FK_blomster_kategori_id=$id";

Why is this throwing me an mysql error 1066?

(also sorry if i am missing some important stuff, this is the first question i am asking on stackoverflow)

回答1:

0.1 seconds of googling: "mysql error 1066" - not unique table name/alias

    FROM
    blomster_produkter   <--table #1
    INNER JOIN blomster_produkter ON   <-table #2

you cannot join a table to itself, or re-use the same table name in a join, without using an alias:

FROM blomster_produkter
INNER JOIN blomster_produkter AS someothername ON
                             ^^^^^^^^^^^^^^^^^--- the alias

and then changing table references as necessary in your join conditions.

As well, note that you are wide open to sql injection attacks. Enjoy having your server pwn3d.



回答2:

Error 1066 is "Not Unique table/alias"

this is because you are join a table with itself without making aliases, you must do aliases like:

SELECT
    bp1.blomster_produkt_id,
    bp1.blomster_produkt_navn,
    bp1.blomster_produkt_pris
    FROM
    blomster_produkter bp1
    INNER JOIN blomster_produkter bp2 ON
    bp1.FK_blomster_produkt_id=bp2.blomster_produkt_navn [...]