简体   繁体   中英

How to build a SQL query to get data from two tables and a serialised column

I have two tables.

Vendor

vendor_id   name    country   city      area
  42        Acme    Thailand  bangkok   sukhumvit
  43        Todo    Thailand  phuket    old town
  45        Goo     Spain     barcelona ramba
  46        Alo     England   London    chelsea
  47        Tchau   Thailand  bangkok   chachukat

Product

product_id  added_by                     
  107       {"type":"vendor","id":"42"}  
  109       {"type":"vendor","id":"44"}  
  110       {"type":"vendor","id":"43"}  
  112       {"type":"vendor","id":"47"} 

I would to have a query that will select all the products from all the vendors from a specific city.

For example. How can I have all the products from all vendors that are located in Bangkok.

Can get products from all vendors that are located in Bangkok using following query.

SELECT Product.product_id
FROM Product
INNER JOIN Vendor ON Product.added_by LIKE CONCAT(  '%', Vendor.vendor_id,  '%' ) 
WHERE Vendor.city =  'bangkok'

Try this:

SELECT product_id FROM Product WHERE added_by = ANY (SELECT vendor_id FROM 
Vendor WHERE city = "bangkok"); (or anything you want)

Maybe it won't serialize it, but it work well.

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