Selecting info in MySQL - php

i want to select all where my account exits
but am getting this error
DBD::mysql::st execute failed: Operand should contain 1 column(s)
USER
ID ACCOUNT
1 JOHN
2 JANE
3 JET
4 KAT
5 YMT
FRIENDS
ID ACCOUNT FRIEND
1 JOHN JET
2 JET JANA
3 KAT JOHN
NOTIC
ID ACCOUNT
1 JOHN
SELECT count(*) FROM notic WHERE account IN (SELECT account, friend FROM friends WHERE account = 'JOHN' OR friend = 'JOHN'

You can join the friends table on notic.account = friend.account or notic.account = friend.friend
Something like this
select count(*) from notic n join friends f on n.account = f.account || n.account = f.friend where n.account = 'JOHN';
Here is a fiddle showing it working

Related

fetching details of user having same reporting email address using join or alias

I have two tables
tbl_user:
id fisrtname lastname
5 John Doe
6 Peter Parker
7 Will Smith
tbl_experience:
exp_id usr_id user_reporting_to
1 5 dev#abc.com
2 6 admin#abc.com
3 7 dev#abc.com
I want to fetch those record who have same reporting email address with where condition usr_id=5 because when i am login in to my page it creates the session of user id for e.g it is now 5 in where condition using join or alias
SELECT texperience.tbl_experience_report_to_email AS tbl_experience_report_to_email,
tuser.tbl_user_fname AS tbl_user_fname,
texperience.tbl_experience_designation AS tbl_experience_designation
FROM tbl_experience AS texperience,
tbl_user AS tuser
WHERE tuser.tbl_user_fname = tuser.tbl_experience_id
AND texperience.tbl_experience_report_to_email = texperience.tbl_experience_id
AND texperience.tbl_experience_user_id = 1
SELECT tbl_user.*, tbl_experience.user_reporting_to FROM tbl_user
LEFT JOIN tbl_experience ON (tbl_user.id=tbl_experience.usr_id)
LEFT JOIN (SELECT count(*) as total_user, user_reporting_to FROM tbl_experience GROUP BY user_reporting_to) as email_group ON
(email_group.user_reporting_to = tbl_experience.user_reporting_to) WHERE email_group.total_user > 1
Basically have a subquery that group all the email address and the joined table will return those users that have emails appearing more than once in the tbl_experience.

Optimal way to store follower and following MySQL

I have 2 tables one is user and another is company. Both have the following columns common while other columns are different.
id email name
1 first#email.com First user
2 second#email.com Second User
3 third#email.com Third User
Now a user can follow many companies but a company does not follow back user. Company should have data with users who follow them and a user should also store the companies they are following.
What is the simplest and optimal to make this happen on MySQL? Either should I create separate table for it or just add a column on existing tables with array of followers. Please answer assuming I have working level of knowledge on PHP and MySQL.
You have the tables User and Company. Let's suppose they contain these values:
User
UserId
UserName
UserMail
1
Alice
alice#mail.com
2
Bob
bob#mail.com
Company
CompanyId
CompanyName
CompanyAddress
1
Microsoft
Redmond
2
Apple
Cupertino
3
Google
Mountain View
Then you should create a many-to-many table - e.g. let's call it UserCompany - which contains at least the columns UserId and CompanyId (coming from the first two tables).
Let's suppose that Alice follows Microsoft and Apple, and Bob follows Google. Your new UserCompany table will contain the following data:
UserId
CompanyId
1
1
1
2
2
3
Company should have data of users who follow them
Here you are the query to get the data of the users that follow Microsoft:
SELECT u.UserName, u.UserMail
FROM User AS u
JOIN UserCompany AS uc ON u.UserId = uc.UserId
WHERE uc.CompanyId = 1
User should also have data of companies they are following.
And here you are the query to get the data of the companies followed by Alice:
SELECT c.CompanyName, c.CompanyCity
FROM UserCompany AS uc
JOIN Company AS c ON c.CompanyId = uc.CompanyId
WHERE uc.UserId = 1
The simplest solution is to have a MANY-TO-MANY join table to link these 2 tables. You can do this with either 2 MANY-TO-MANY tables or a single one.
For example 2 table:
user_follows_company
ID CompanyID UserID
---- ------ ---------
1 1 5
1 1 6
1 1 8
1 2 5
You can get the companies a user follows by using the following SQL:
SELECT * FROM company c JOIN user_follows_company ufc on ufc.companyID = c.ID WHERE user = $USER_ID
company_follows_user
ID CompanyID UserID
---- --------- ---------
1 3 5
1 3 6
1 5 3
1 5 4
You can get the users a company follows by using the following SQL:
SELECT * FROM user u JOIN company_follow_user cfu on cfu.userID = u.ID WHERE user = $USER_ID
or you can do this with a single table and have a column that designates which direction is being followed:
follows_table
ID CompanyID UserID Initiator
---- --------- --------- ---------
1 3 5 Company
1 3 6 Company
1 5 3 Company
1 5 4 Company
1 5 1 User
1 6 1 User
1 8 1 User
1 5 2 User
Note that storing the "initiator" as a string is not a good idea - it probably should be an int or an ENUM
To query on this table, do the following queries:
Companies a user follows:
SELECT * FROM company c JOIN follows_table ft on ft.userID = c.ID WHERE user = $USER_ID AND ft.initiator = 'user'
Users a company follows:
SELECT * FROM user u JOIN follows_table ft on ft.userID = u.ID WHERE companyID = $$COMPANY_ID AND ft.initiator = 'company'

List suggested friends

I'm trying to get a list of suggested friends that are not within my friends but are in my friends, friends.
status of 2 being an accepted friend.
My session id is 34 and I'm friends with user 3 and user 3 is friends with user 16, so user 16 would show as a suggested friend as user 16 is not my friend.
Friends table
id | user 1_id | user2_id | status
----------------------------------
4 3 34 2
3 34 3 2
2 3 16 2
1 16 3 2
Query (What I've tried)
$user1_id=$_SESSION['id'];
$user2_id=$data['id'];
$collectmutualfriendsa = mysqli_query($mysqli,"
SELECT DISTINCT r2.user1_id
FROM
friends r
INNER JOIN friends r2
ON r.user1_id = r2.user2_id
LEFT OUTER JOIN friends r3 ON r3.user2_id = r2.user1_id AND r3.user1_id=2
WHERE r.user2_id = 2 AND r3.user1_id is null");
$user1_id = $_SESSION['id']; // 34
$user2_id = $data['id']; // 3
$query = "SELECT * FROM Friends_table WHERE user1_id = '$user2_id' AND user2_id != $user1_id;";
Okay, I tried a UNION and now have it working.
$collectmutualfriendsa = mysqli_query($mysqli,"
SELECT *
FROM friends
WHERE user2_id NOT IN
(
SELECT user1_id FROM friends WHERE user2_id = '$user1_id'
UNION
SELECT user2_id FROM friends WHERE user2_id = '$user1_id'
)
");

PHP Mysql select query to order pairs

I'm making a customer database application for a dancing school.
I need to display an overview of all customers that are participating in the same dancing level. But I want the overview order by couples not by the customer ID.
For this I'm joining three tables (look at the query below)
Each customer has a unique ID in the tabel CRM_CONTACTS and also has in it's row a reference to his or her partner (PARTNER_ID).
Table CRM_CONTACTS
ID CONTACTS_LNAME
1 VON KLOMPENBURG
2 Mc Donalds
3 MC Adams
4 Mr X
Then I have CRM_PRODUCTS
ID PRODUCTS_NAME
1 Beginners
2 intermediate
3 advanced
Then the table in which I assign a product/level to a contact and also indicate his/her partner
ID CONTACTS_ID PRODUCTS_ID PARTNER_ID
1 1 1 4
2 2 1 3
3 3 1 2
4 4 1 1
Now I would like to receive a list order by couple based on the parter_id
So for the beginners level I would get a list like this
1 VON KLOMPENBURG
2 Mr X
3 Mc Donalds
4 Mc Adams
Here's my select statement
$result = mysqli_query($coni,"SELECT CRM_PRODUCTS_PURCHASE.ID, CONTACTS_ID,
CRM_PRODUCTS.PRODUCTS_NAME,
CRM_PRODUCTS.PRODUCTS_PRICE,CRM_PRODUCTS_PURCHASE.PARTNER_ID,
CRM_PRODUCTS_PURCHASE.PARTNER_NAME,
PRODUCTS_PURCHASE_REMARKS,PRODUCTS_PURCHASE_DISCOUNT,
PRODUCTS_PURCHASE_PAIDBYBANK,PRODUCTS_PURCHASE_PAIDBYCASH,
CRM_CONTACTS.CONTACTS_LNAME, CRM_CONTACTS.CONTACTS_FNAME
FROM CRM_PRODUCTS_PURCHASE
LEFT JOIN CRM_CONTACTS ON CRM_PRODUCTS_PURCHASE.CONTACTS_ID = CRM_CONTACTS.ID
LEFT JOIN CRM_PRODUCTS ON CRM_PRODUCTS_PURCHASE.PRODUCTS_ID = CRM_PRODUCTS.ID
WHERE CRM_PRODUCTS_PURCHASE.PRODUCTS_ID = '". $PRODUCTS_ID . "'");
You can do something like this:
$result = mysqli_query($coni,"SELECT CRM_PRODUCTS_PURCHASE.ID, CONTACTS_ID,
CRM_PRODUCTS.PRODUCTS_NAME,
CRM_PRODUCTS.PRODUCTS_PRICE,CRM_PRODUCTS_PURCHASE.PARTNER_ID,
CRM_PRODUCTS_PURCHASE.PARTNER_NAME,
PRODUCTS_PURCHASE_REMARKS,PRODUCTS_PURCHASE_DISCOUNT,
PRODUCTS_PURCHASE_PAIDBYBANK,PRODUCTS_PURCHASE_PAIDBYCASH,
CRM_CONTACTS.CONTACTS_LNAME, CRM_CONTACTS.CONTACTS_FNAME
FROM CRM_PRODUCTS_PURCHASE
LEFT JOIN CRM_CONTACTS ON CRM_PRODUCTS_PURCHASE.CONTACTS_ID = CRM_CONTACTS.ID
LEFT JOIN CRM_PRODUCTS ON CRM_PRODUCTS_PURCHASE.PRODUCTS_ID = CRM_PRODUCTS.ID
WHERE CRM_PRODUCTS_PURCHASE.PRODUCTS_ID = '". $PRODUCTS_ID . "'
ORDER BY IF (ID < PARTNER_ID, ID, PARTNER_ID)");
And your customers will be ordered in couples.

mysql join 2 user_ids in a row from users table

There are 2 tables:
orders:
id requester_id supplyer_id status
1 423 1 reserved
2 500 1 supplied
3 222 2 reserved
...
users
id username register_date
1 admin 2012-01-01
2 smith 2013-01-01
...
423 John 2012-10-11
500 Doe 2012-12-11
222 name 2012-10-13
...
I want to join these two tables and get this as a result
id requester_username supplier_username status
1 John admin reserved
2 Doe admin supplied
3 name smith reserved
I can actually join these tables using active records like :
$this->db->select('orders.*,users.username')
->from('orders')
->join('users','users.id = orders.requester_id')
->get()->result();
but I dont know how to get the supplier username at the same time.
Try adding a second JOIN:
$this->db->select('orders.*,requesters.username,suppliers.username')
->from('orders')
->join('users AS requesters', 'requesters.id = orders.requester_id')
->join('users AS suppliers', 'suppliers.id = orders.supplier_id')
->get()->result();
In MySql Your query should be like -
Select a.id, b.username, c.username, a.status
from orders a, users b, users c
where
a.requester_id = b.id
And
a.supplyer_id = c.id
Thanks

Categories