Your first query
A tasks table and three queries; MySQL has no RETURNING, so the insert is execresult and LastInsertId hands back the AUTO_INCREMENT id.
22 examples, in learning order. Unqualified names resolve in the connection's database; RETURNING is off unless enabled.
A tasks table and three queries; MySQL has no RETURNING, so the insert is execresult and LastInsertId hands back the AUTO_INCREMENT id.
One query per command kind on a harbour's boats, from execresult's LastInsertId to exec's bare error.
A ferry timetable whose Params structs take their field names from inserted and compared columns and from binding lines, with ?1 used twice.
Ringed gulls and their sightings, where a nullable column, a LEFT JOIN and MAX give pointers while COALESCE and COUNT do not.
One landing of fish read back column by column, printing the Go type each MySQL column type became, unsigned integers and DECIMAL included.
Chip shop orders kept in JSON columns, encoded into the generated JSON type and searched with -> and ->>, with MySQL handing back its own normalised text.
A chip shop menu with two MySQL ENUM columns, read back as the named enum types Seagull generates and, through a column override and sgl.column, as the shop's own Go types.
Beach huts found and booked by passing Go slices to IN lists, including an empty slice and a slice after a scalar.
One boat trip search run four ways, where each optional filter is a pointer field and a nil pointer switches its filter off.
Regatta entries listed in the order a caller picks from generated constants, with raw text adding no ORDER BY at all.
Rock pool finds read a page at a time, with a default and a maximum page size, then searched with an optional filter and a page.
Unclaimed lost property searched with filters, an order, a limit and a count chosen at run time, with unsafe columns, operators and directions refused.
Boats read with their harbour as a nested value through an INNER JOIN and their skipper as a nil-able pointer through a LEFT JOIN.
Chip shop orders collapsed from a LEFT JOIN into one row each, with their lines as a slice, by group_by.
Tide gauge readings ranged over one row at a time with a range-over-func iterator, once to the end and once stopping early.
A gull colony read through a recursive CTE, a CTE feeding RANK and SUM window functions, and a view, with types and nullability carried through each.
INSERT ... ON DUPLICATE KEY UPDATE adds to an existing row through VALUES(), and its affected-row count of 1 or 2 says whether the row was inserted or updated.
A batch query takes a slice of Params and writes it as multi-row INSERTs, 16,383 four-column rows a statement to stay under MySQL's 65,535 bind variables.
RunInTx commits a transfer when its function returns nil and rolls it back on an error, and WithTx binds the queries to a transaction you begin yourself.
The migration files are embedded in the binary, and each start-up runs migrate.NewMigrator with the MySQL backend and Up, which applies only what the database lacks.
Stored functions created in a migration type the queries that call them, and their data-access characteristic, body or a sgl.migration(readonly:) line decides whether each query goes to the writer or the reader.
A harbour register kept with INSERT IGNORE, REPLACE INTO, a SET column read with FIND_IN_SET, unsigned integers and GROUP_CONCAT, printing what each statement did.
Some ideas belong to other engines, and some need a feature MySQL does not have. Each one says why, and where to read it instead.
MySQL has no RETURNING; use execresult and LastInsertId. Q052
See it on SQLite →