简体   繁体   中英

How to get 2 fields from MySQL many-to-many relationship?

I have such tables in DB:

transaction_item
----------------
transaction_id
item_id

transactions
----------------
id
created_at

store_items
----------------
id
price

I need to get object {created_at: price} . This is 'many to many', so, transaction_item is the main table.

PS I wrote something like object {created_at: price} because I use knex.raw to get it.

You could use two joins:

SELECT created_at, price
FROM   transactions t
JOIN   transaction_item ti ON t.id = ti.transaction_id
JOIN   stote_item i ON i.id = ti.item_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM