Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to query from multiple tables in sqlite.swift

Tags:

sqlite.swift

Is it possible to write a statement in sqlite.swift that will generate the equivalent sql:

SELECT foods.name, food_types.name FROM foods, food_types WHERE foods.type_id=food_types.id LIMIT 10;

I can't figure out how to query from multiple Table objects at once. Thanks!

like image 712
docho Avatar asked Dec 06 '25 14:12

docho


1 Answers

Your original query passes two tables to the FROM clause, creating an implicit join. SQLite.swift's query builder language currently only supports explicit joins.

Check out the documentation under Joining Other Tables for more information on joining tables.

In your case:

let foods = Table("foods")
let food_types = Table("food_types")
let name = Expression<String>("name")
let id = Expression<Int64>("id")
let type_id = Expression<Int64>("type_id")

let query = foods
    .select(foods[name], food_types[name])
    .join(food_types, on: foods[type_id] == food_types[id])
    .limit(10)
like image 88
stephencelis Avatar answered Dec 12 '25 13:12

stephencelis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!