mysql - How can I get all business data AS WELL as if current user is following them? -


in mysql how can write query fetch business data, , @ same time (or not if better way) check if user following business? have following relationship table determine if user following business (status=1 mean person following):

create table if not exists `relationship_user_follows_business` (   `user_id` int(10) unsigned not null,   `business_id` int(10) unsigned not null,   `status` tinyint(3) unsigned not null default '0' comment '1=following, 0=not following' ) engine=innodb default charset=utf8mb4;  alter table `relationship_user_follows_business`   add unique key `unique_user_business_id` (`user_id`,`business_id`); 

assume business table holds data on different businesses name, phone number, etc. want return of business data in query (business.*). want append status (0 or 1) end of each business row determine if user following business. have tried following query not work because narrowing results show business if there relationship row. wish show businesses regardless if relationship row exists or not because create relationship row if user follows:

select business.*, relationship_user_follows_business.status business, relationship_user_follows_business 104=relationship_user_follows_business.user_id , business.id=relationship_user_follows_business.business_id 

note using 104 test user id. user id dependent on user, not static 104.

you looking left join , not inner join keeps all records master table , matching rows details table . also, avoid using implicit join syntax(comma separated) , use proper syntax of join :

select business.*, relationship_user_follows_business.status   business   left join relationship_user_follows_business     on business.id = relationship_user_follows_business.business_id      , relationship_user_follows_business.user_id = 104 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -