Oh, thank for you excellent explain, i almost thought i found a bug! 在2021年11月11日星期四 UTC+8 下午2:18:00<Jeremy Evans> 写道:
> On Wed, Nov 10, 2021 at 7:14 AM Billy Zheng <vil...@gmail.com> wrote: > >> Please see a example. >> >> I have a table, name institutions, and exists a :name column. >> >> Following is SQL generate by Sequel, please notice, there is a alias like >> this: >> >> stock"."name" AS "stock_name" >> > > This is expected. Without this alias, the identifier symbol used for the > returned column would be "name", which would clash with the "name" from > "institutions"."name". Sequel's graphing code always tries to generate > unique aliases. > > >> ``` >> SELECT "institutions"."id", "institutions"."name", >> "institutions"."number_of_holding", "instit >> utions"."market_value", "institutions"."percent_of_shares_for_stock", >> "institutions"."percent_ >> of_shares_for_institution", "institutions"."quarterly_changes_percent", >> "institutions"."quarte >> rly_changes", "institutions"."market_value_dollar_string", >> "institutions"."holding_cost", "ins >> titutions"."date", "institutions"."created_at", >> "institutions"."stock_id", "institutions"."fir >> m_id", "stock"."id" AS "stock_id_0", "stock"."name" AS "stock_name", >> "stock"."exchange_id", "s >> tock"."percent_of_institutions", "stock"."ipo_price", >> "stock"."ipo_amount", "stock"."ipo_place >> ment", "stock"."ipo_date", "stock"."ipo_average_price", >> "stock"."ipo_placement_number", "stock >> "."next_earnings_date", "exchange"."id" AS "exchange_id_0", >> "exchange"."name" AS "exchange_nam >> e", "firm"."id" AS "firm_id_0", "firm"."name" AS "firm_name", >> "firm"."display_name" FROM "inst >> itutions" LEFT OUTER JOIN "stocks" AS "stock" ON ("stock"."id" = >> "institutions"."stock_id") LE >> FT OUTER JOIN "exchanges" AS "exchange" ON ("exchange"."id" = >> "stock"."exchange_id") LEFT OUTE >> R JOIN "firms" AS "firm" ON ("firm"."id" = "institutions"."firm_id") >> ORDER BY "stock"."name" >> ``` >> >> Then i remove :name column use sequel migration, then run same code, get >> following new SQL. >> > > In this query, there is no "institutions"."name", so there is no reason to > alias "stock"."name" in order to disambiguate. > > >> >> SELECT "institutions"."id", "institutions"."number_of_holding", >> "institutions"."market_value", >> "institutions"."percent_of_shares_for_stock", >> "institutions"."percent_of_shares_for_instituti >> on", "institutions"."quarterly_changes_percent", >> "institutions"."quarterly_changes", "institut >> ions"."market_value_dollar_string", "institutions"."holding_cost", >> "institutions"."date", "ins >> titutions"."created_at", "institutions"."stock_id", >> "institutions"."firm_id", "stock"."id" AS >> "stock_id_0", "stock"."name", "stock"."exchange_id", >> "stock"."percent_of_institutions", "stock >> "."ipo_price", "stock"."ipo_amount", "stock"."ipo_placement", >> "stock"."ipo_date", "stock"."ipo >> _average_price", "stock"."ipo_placement_number", >> "stock"."next_earnings_date", "exchange"."id" >> AS "exchange_id_0", "exchange"."name" AS "exchange_name", "firm"."id" AS >> "firm_id_0", "firm". >> "name" AS "firm_name", "firm"."display_name" FROM "institutions" LEFT >> OUTER JOIN "stocks" AS " >> stock" ON ("stock"."id" = "institutions"."stock_id") LEFT OUTER JOIN >> "exchanges" AS "exchange" >> ON ("exchange"."id" = "stock"."exchange_id") LEFT OUTER JOIN "firms" AS >> "firm" ON ("firm"."id >> " = "institutions"."firm_id") ORDER BY "stock"."name" >> >> The strange things is, why the first SQL generate stock"."name" AS >> "stock_name", and the second SQL not create alias like "AS "stock_name" ? >> > > It's needed for disambiguation in the first case, but not in the second > case. > > Thanks, > Jeremy > -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to sequel-talk+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/5b3c0027-5d63-423a-9be0-9fb5ad39b590n%40googlegroups.com.