Note: if you are using MySQL 8.0.16 and below you won't be able to alias your DELETE table (https://bugs.mysql.com/bug.php?id=89410).
SQLDELETE FROM public.users
WHERE email = 'bob@email.com';
Go
u := tables.USERS()
// ...if you use ToSQL
query, args := sq.DeleteFrom(u).
Where(u.EMAIL.EqString("bob@email.com")).
ToSQL()
_, err := DB.Exec(query, args...)
if err != nil {
/* handle error */
}
// ...if you use Exec
_, err := sq.DeleteFrom(u).
Where(u.EMAIL.EqString("bob@email.com")).
Exec(DB, 0)
if err != nil {
/* handle error */
}
In MySQL, if you are using JOINs I recommend you do not alias the table you are deleting from. Doing so will necessitate a different query structure, and that might trip you up. For completion, both examples (aliased and unaliased) will be shown below.
For Postgres, it doesn't matter. You can alias or not alias the table, the query structure stays the same.
SQL--------------
-- Postgres --
--------------
DELETE FROM public.users AS u
USING public.user_roles AS ur
JOIN public.user_roles_students AS urs ON urs.user_role_id = ur.user_role_id
WHERE u.user_id = ur.user_id AND urs.team_id = 15;
-----------
-- MySQL --
-----------
-- unaliased
DELETE FROM public.users
JOIN public.user_roles AS ur ON ur.user_id = users.user_id
JOIN public.user_roles_students AS urs ON urs.user_role_id = ur.user_role_id
SET u.name = 'bob', u.password = '1234'
WHERE urs.team_id = 15;
-- aliased
DELETE FROM u
USING public.users AS u
JOIN public.user_roles AS ur ON ur.user_id = u.user_id
JOIN public.user_roles_students AS urs ON urs.user_role_id = ur.user_role_id
SET u.name = 'bob', u.password = '1234'
WHERE urs.team_id = 15;
Go
//----------//
// Postgres //
//----------//
u := tables.USERS().As("u")
ur := tables.USER_ROLES().As("ur")
urs := tables.USER_ROLES_STUDENTS().As("urs")
// ...if you use ToSQL
query, args := sq.DeleteFrom(u).
Using(ur).
Join(urs, urs.USER_ROLE_ID.Eq(ur.USER_ROLE_ID)).
Where(u.USER_ID.Eq(ur.USER_ID), urs.TEAM_ID.EqInt(15)).
ToSQL()
_, err := DB.Exec(query, args...)
if err != nil {
/* handle error */
}
// ...if you use Exec
_, err := sq.Update(u).
Using(ur).
Join(urs, urs.USER_ROLE_ID.Eq(ur.USER_ROLE_ID)).
Where(u.USER_ID.Eq(ur.USER_ID), urs.TEAM_ID.EqInt(15)).
Exec(DB, 0)
if err != nil {
/* handle error */
}
//-------//
// MySQL //
//-------//
// unaliased
u := tables.USERS()
ur := tables.USER_ROLES().As("ur")
urs := tables.USER_ROLES_STUDENTS().As("urs")
// ...if you use ToSQL
query, args := sq.DeleteFrom(u).
Join(ur, ur.USER_ID.Eq(u.USER_ID)).
Join(urs, urs.USER_ROLE_ID.Eq(ur.USER_ROLE_ID)).
Where(urs.TEAM_ID.EqInt(15)).
ToSQL()
_, err := DB.Exec(query, args...)
if err != nil {
/* handle error */
}
// ...if you use Exec
_, err := sq.DeleteFrom(u).
Join(ur, ur.USER_ID.Eq(u.USER_ID)).
Join(urs, urs.USER_ROLE_ID.Eq(ur.USER_ROLE_ID)).
Where(urs.TEAM_ID.EqInt(15)).
Exec(DB, 0)
if err != nil {
/* handle error */
}
// aliased
u := tables.USERS().As("u")
ur := tables.USER_ROLES().As("ur")
urs := tables.USER_ROLES_STUDENTS().As("urs")
// ...if you use ToSQL
query, args := sq.DeleteFrom(u).
Using(u).
Join(ur, ur.USER_ID.Eq(u.USER_ID)).
Join(urs, urs.USER_ROLE_ID.Eq(ur.USER_ROLE_ID)).
Where(urs.TEAM_ID.EqInt(15)).
ToSQL()
_, err := DB.Exec(query, args...)
if err != nil {
/* handle error */
}
// ...if you use Exec
_, err := sq.DeleteFrom(u).
Using(u).
Join(ur, ur.USER_ID.Eq(u.USER_ID)).
Join(urs, urs.USER_ROLE_ID.Eq(ur.USER_ROLE_ID)).
Where(urs.TEAM_ID.EqInt(15)).
Exec(DB, 0)
if err != nil {
/* handle error */
}
Note: Multi-table DELETE is a mysql-only feature.
Similar to multi-table UPDATE, MySQL can also do multi-table DELETE. However, I've found it to be of limited practicality due to foreign key constraints. The order of table deletion is not controllable, and the query optimizer may decide on an order that violates a foreign key constraint and cause the DELETE to fail. The MySQL docs mention that the only way to get around this is to do single-table DELETEs.
You will not be able to use the ORDER BY and LIMIT clauses in a multi-table DELETE.
SQLDELETE FROM u, ur, urs
USING public.users AS u
JOIN public.user_roles AS ur ON ur.user_id = u.user_id
JOIN public.user_roles_students AS urs ON urs.user_role_id = ur.user_role_id
WHERE urs.team_id = 15;
Go
u := tables.USERS().As("u")
ur := tables.USER_ROLES().As("ur")
urs := tables.USER_ROLES_STUDENTS().As("urs")
// ...if you use ToSQL
query, args := sq.DeleteFrom(u, ur, urs).
Using(u).
Join(ur, ur.USER_ID.Eq(u.USER_ID)).
Join(urs, urs.USER_ROLE_ID.Eq(ur.USER_ROLE_ID)).
Where(urs.TEAM_ID.EqInt(15)).
ToSQL()
_, err := DB.Exec(query, args...)
if err != nil {
/* handle error */
}
// ...if you use Exec
_, err := sq.DeleteFrom(u, ur, urs).
Using(u).
Join(ur, ur.USER_ID.Eq(u.USER_ID)).
Join(urs, urs.USER_ROLE_ID.Eq(ur.USER_ROLE_ID)).
Where(urs.TEAM_ID.EqInt(15)).
Exec(DB, 0)
if err != nil {
/* handle error */
}
Clauses are just struct fields, you can either use method chaining to add clauses or manipulate the struct fields directly.
var q sq.InsertQuery
// Adding
q = q.With(cte1, cte2, cte3)
// is equivalent to //
q = q.With(cte1).With(cte2).With(cte3)
// is equivalent to //
q.CTEs = []sq.CTE{cte1, cte2, cte3}
// Removing
q.CTEs = nil
var q sq.DeleteQuery
// Adding
q = q.DeleteFrom(table)
// is equivalent to //
q.FromTable = table
// Removing
q.FromTable = nil
var q sq.DeleteQuery
// Adding
/* from one table */
q = q.DeleteFrom(table)
// is equivalent to //
q.FromTables = []sq.BaseTable{table}
/* from multiple tables */
q = q.DeleteFrom(table1, table2, table3)
// is equivalent to //
q = q.DeleteFrom(table1).DeleteFrom(table2).DeleteFrom(table3)
// is equivalent to //
q.FromTables = []sq.BaseTable{table1, table2, table3}
// Removing
q.FromTables = nil
var q sq.DeleteQuery
// Adding
q = q.Using(table)
// is equivalent to //
q.UsingTable = table
// Removing
q.UsingTable = nil
var q sq.DeleteQuery
// Adding
q = q.Join(table1, predicate1).
LeftJoin(table2, predicate2).
FullJoin(table3, predicate3)
// is equivalent to //
q.JoinTables = []sq.JoinTable{
sq.Join(table1, predicate1),
sq.LeftJoin(table2, predicate2),
sq.FullJoin(table3, predicate3),
}
// Removing
q.JoinTables = nil
var q sq.DeleteQuery
// Adding
q = q.Where(predicate1, predicate2, predicate3)
// is equivalent to //
q = q.Where(predicate1).Where(predicate2).Where(predicate3)
// is equivalent to //
q.WherePredicate.Predicates = []sq.Predicate{
predicate1,
predicate2,
predicate3,
}
// Removing
q.WherePredicate.Predicates = nil
var q sq.DeleteQuery
// Adding
q = q.OrderBy(field1, field2, field3)
// is equivalent to //
q = q.OrderBy(field1).OrderBy(field2).OrderBy(field3)
// is equivalent to //
q.OrderByFields = []sq.Field{field1, field2, field3}
// Removing
q.OrderByFields = nil
var q sq.DeleteQuery
// Adding
q = q.Limit(10)
// is equivalent to //
limit := int64(10)
q.LimitValue = &limit
// Removing
q.LimitValue = nil
var q sq.DeleteQuery
// Adding
q = q.Returning(field1, field2, field3)
// is equivalent to //
q = q.Returning(field1).Returning(field2).Returning(field3)
// is equivalent to //
q.ReturningFields = []sq.Field{field1, field2, field3}
// Removing
q.ReturningFields = nil