2

What the absolute fuck

Can someone explain why `SELECT foobar FROM sometable` works in MariaDB while `SELECT sometable.foobar` doesn't?

The error is "code 1109. Unknown table foobar in field list"

Comments
  • 2
    Did you select the database?

    EDIT: Maybe you didn't do a “USE database” before, so MariaDB takes sometable as a database name since there is no current database selected.
  • 0
    The database is 'use'ed and the official docs (https://mariadb.com/kb/en/...) say "When specifying a column, you can either use just the column name or qualify the column name with the name of the table using tbl_name.col_name"
  • 0
    Okay I figured it out. You have to actually specify the table in the FROM list, i.e. `SELECT sometable.foobar FROM sometable;`
  • 1
    @12bitfloat wait so in some SQL databases
    SELECT table.colum alone is allowed?

    I've always thought that table.column is allowed to make reading join queries a lot easier or at least specifying from which table that column is to be fetched in-case same column name exist in multiple tables of the same join query
  • 1
    @gitpush I don't think it's really a thing. I guess I just remembered it wrong
  • 0
    @gitpush its to specify the tables when you are using joins
  • 2
    You can specific table names or add alias' to your tables

    @highlight

    # join with full table names

    SELECT table1.foo, table2.bar
    FROM table1
    JOIN table2 ON (table1.id = table2.fk)
    WHERE table2.someDate = "2019-10-02"

    # otherwise using an alias for tables

    SELECT t1.foo, t2.bar
    FROM table1 t1
    JOIN table2 t2 ON (t1.id = t2.fk)
    WHERE t2.someDate = "2019-10-02"
  • 0
  • 0
    @C0D4 That's actually what I normally do. Today at work I had to make a query that fetches the newest timestamp from like 12 different tables and I didn't want to have to specify the table names twice
Add Comment