问题
seller:
id, age, origin, gender, buyer_age, buyer_origin, buyer_gender
buyer:
id, age, origin, seller_age, seller_origin, seller_gender
Here we want to suggest the buyer to buy from this seller
AND
suggest the seller to sell things to the buyer
with cronjob.
Doing it with one query is preferred instead of foreach seller. What's the most efficient way to do it? I don't want to SELECT each one and get info then change the query and again SELECT that's a killing process in coding and using system memory. If there's a possibility of using join or so with below statement then it's OK.
If seller or buyer says age and origin is NOT important for me, then how is it possible? Is there
WHERE seller_origin = ANY
in mysql?
回答1:
Firstly change all the column names in both buyer and seller to be prefixed with the name of the table in which they originate as we will be joining tables.
SELECT *
FROM seller
INNER JOIN buyer
ON
(seller.buyer_gender = buyer.gender OR seller.buyer_gender IS NULL) AND
(buyer.seller_gender = buyer.gender OR buyer.seller_gender IS NULL) AND
(seller.buyer_origin = buyer.origin OR seller.buyer_origin IS NULL) AND
(buyer.seller_origin = seller.origin OR buyer.seller_origin IS NULL) AND
(seller.buyer_age = buyer.age OR seller.buyer_age IS NULL) AND
(buyer.seller_age = seller.age OR buyer.seller_age IS NULL)
来源:https://stackoverflow.com/questions/31214144/organize-database-multichoice-columns-by-cronjob