SELECT multiple table - php

i need some help here,
table "friends"
+------+-------------+-----------+
id | friend_id | user_id |
+------+-------------+-----------+
1 | 1222 | 99999 |
+------+-------------+-----------+
2 | 48989 | 1492 |
+------+-------------+-----------+
table "users"
+------+-------------+---------------+
id | user_name | user_image |
+------+-------------+---------------+
99999 | Mark | img/abc.jpg |
+------+-------------+---------------+
1222 | Tom | img/xyz.jpg |
+------+-------------+---------------+
etc. | etc. | etc.. |
+------+-------------+---------------+
i want SELECT table friends and make WHERE statement :
etc : ... WHERE user_id=$_SESSION[user_id] ...
and will display data from table users
ok , let say :
my current id is 99999 so in table friends is match only 1222 , so this will display all data(image,etc..) from id 1222(Tom) from table users.
So my question here is how i need to write this code for generate users data ?
*i try to use UNION and LEFT JOIN..but no luck..still newbie..

$user_id = intval($_SESSION['user_id']);
$friends_of_user = mysql_query('
SELECT
f.*, u.*
FROM
friends f
LEFT JOIN
users u
ON
u.id = f.friend_id
WHERE
f.user_id = '.$user_id);
and to exclude all users which doesn't have profiles in users table, just change LEFT JOIN to JOIN

I'd use the following code:
$user_id = mysql_real_escape_string($_SESSION['user_id']);
$sql = "SELECT f.*
FROM users u
INNER JOIN friends f ON (u.user_id = f.friend_id)
WHERE u.user_id = '$user_id' ";

Related

How do I select distinct rows and cross check in another table?

I have 3 tables, users and tasks and completed_tasks. So basically I want to select all tasks where the user_id = 2 AND also check that the task does not exist in another table` the So here is my tables:
users table:
+----+-------+
| id | name |
+----+-------+
| 1 | John |
| 2 | Sally |
+----+-------+
tasks table:
+----+-----------+---------+
| id | task_name | user_id |
+----+-----------+---------+
| 1 | mop floor | 2 |
| 2 | dishes | 1 |
| 3 | laundry | 2 |
| 4 | cook | 2 |
+----+-----------+---------+
completed_tasks table:
+----+---------+---------+
| id | task_id | user_id |
+----+---------+---------+
| 1 | 1 | 2 |
+----+---------+---------+
Here is my current SELECT code for my MySQL database:
$db = "SELECT DISTINCT tasks.task_name, users.name FROM tasks LEFT JOIN ON users.id = tasks.user_id WHERE tasks.user_id = 2";
THe problem I'm having is: I want it to search in completed_tasks table and if the task exists, then don't select that task.
I tried to do that by adding the following but it did not work:
LEFT JOIN completed_tasks ON completed_tasks.user_id = 2
That did not work because if I had multiple completed tasks, it would just ignore it all together.
I want the end result should return the user's name and task name of task 3 and 4.
Also, performance is critical in my application. I could use PHP and loop through the arrays and do SELECT for each of them but that would not be good for performance.
You have a few ways to do this.
You can use a LEFT JOIN and then check for NULL in the optional table.
SELECT a.name, b.task_name
FROM users a
JOIN tasks b ON a.id = b.user_id
LEFT JOIN completed_tasks c ON c.task_id = b.id AND c.user_id = b.user_id
WHERE c.id IS NULL
;
You can do a NOT EXISTS sub-query
SELECT a.name, b.task_name
FROM users a
JOIN tasks b ON a.id = b.user_id
WHERE NOT EXISTS (
SELECT 1
FROM completed_tasks c
WHERE c.task_id = b.id AND c.user_id = b.user_id)
;

MYSQL Select from tables based on multiple rows

I have a table called user_meta. In that table I have the following columns: ID, userID, meta_key, meta_value
I have another table called users, the only important column there is ID, which I want to compare to the user_meta table rows.
The users table looks like:
ID | email | etc...
1 | email#test.com |
5 | testa#a.com |
6 | .... |
7 | .... |
So say I have a table (user_meta) that looks like:
ID | userID | meta_key | meta_value
2 | 1 | companyID | 2
3 | 1 | user_type | staff
4 | 5 | companyID | 2
5 | 5 | user_type | staff
6 | 6 | companyID | 4
7 | 6 | user_type | customer
I want to retrieve a single row for each userID, but only if the company ID and user_type are correct.
I want to retrieve all users that have the same companyID that I would send in the query, so let's say $companyID=2, and then all users that have the user_type='staff'.
So user_meta.userID must equal users.ID, and user_meta.companyID must equal 2, and user_meta.user_type must equal 'staff'.
I want a list of all users that match these criteria.
A result would be userID 1 & 5 are returned. They both have companyID = 2, and both have user_type = staff
You need to join with user_meta once for each attribute you want to match.
SELECT u.*
FROM users AS u
JOIN user_meta AS m1 ON u.id = m1.userID
JOIN user_meta AS m2 ON u.id = m2.userID
WHERE m1.meta_key = 'companyID' AND m1.meta_value = :companyID
AND m2.meta_key = 'user_type' AND m2.meta_value = 'staff'
Not very sure about your question. I'm assuming this is what you may want:
select * from Users where ID in (
select userID from user_meta where (meta_key = 'companyID' and meta_value = 2) or (meta_key = 'user_type' and meta_value = 'staff')
);
SELECT `users`.`id`,
`Company`.`meta_value`,
`UserType`.`meta_value`
FROM `users`
JOIN `user_meta` `Company`
ON `Company`.`userid` = `users`.`id`
JOIN `user_meta` `UserType`
ON `UserType`.`userid` = `users`.`id`
WHERE `UserType`.`meta_value` = 'staff'
AND `Company`.`meta_value` = 2
https://gyazo.com/de8d9124418f65b993d708c80c309325

trying to update database flag if condition user id exist in another table using cronjob

I am trying to update " user table starred flag to 1 " if id of users table exist in user_pages,
Here are both table structures
1- users table
| id | starred |
| 1 | 0 |
2- user_pages table
| uid |
| 2 |
| 5 |
| 1 |
I am trying to do it with this cron command :
$query="update users up join
users u
on up.id = u.id
set up.starred = (case when u.id = uid then 1 else 0 end)";
mysql_query($query);
Expected Results
| id | starred |
| 1 | 1 |
Please help me out to update a flag if id exist in another table
Note::
Id is = id in users table
Id is = uid in user_pages table
update user u join user_pages up on u.id = up.uid
set u.starred = 1 where u.id in (select uid from user_pages);
Here's one simple way:
UPDATE users u
JOIN user_pages up ON up.uid = u.id
SET u.starred = 1;
UPDATE users a
JOIN user_pages b ON a.id = b.uid
SET a.starred = 1 where a.id = 1 //or your update id
users.starred is updated only, if uid exists in uses_pags table

How do I make this select query?

I want to create a SELECT query in mysql.
There are two tables, users and image_info, and need to select following columns.
user table : user_id, username, dob
image_info : image, image_path
When selecting image. I need to get only primary image from image_info table. In image_info table there is a column like:
image_type ENUM('primary', 'gallery') DEFAULT NULL,
This is how I tried it..
$q = "SELECT u.user_id, u.username, u.dob, i.image, i.image_path
FROM users u
INNER JOIN image_info i ON i.user_id = u.user_id
WHERE u.sex = 'Male'
ORDER BY u.date_registered DESC LIMIT 6";
But it doesn't work properly to get my expected output.
UPDATE:
my table outputs..
mysql> select user_id, username, sex from users;
+---------+-------------+--------+
| user_id | username | sex |
+---------+-------------+--------+
| 1 | thara1234 | Male |
| 2 | root234 | Male |
| 3 | kamal123 | Female |
| 4 | Nilantha | Male |
| 5 | Ruwan324324 | Male |
+---------+-------------+--------+
5 rows in set (0.00 sec)
mysql> select user_id, image, image_type from image_info;
+---------+----------------------------+------------+
| user_id | image | image_type |
+---------+----------------------------+------------+
| 2 | 2_root234_1433564588.jpg | primary |
| 1 | 1_thara1234_1433555104.jpg | primary |
| 1 | 1_thara1234_1433556481.jpg | gallery |
| 4 | 4_Nilantha_1433573768.jpg | primary |
+---------+----------------------------+------------+
4 rows in set (0.03 sec)
Thank you.
I think, query would be :-
SELECT User.user_id, User.username, User.dob, Image.image, Image.image_path
FROM
users User LEFT JOIN image_info Image
ON User.user_id = Image.user_id AND Image.image_type = 'PRIMARY'
WHERE User.sex= 'Male'
ORDER BY User.date_registered DESC LIMIT 6
As you said you need the user either he has an image or not you should use left join in your query:
SELECT u.user_id, u.username, u.dob, i.image, i.image_path
FROM users u
LEFT JOIN image_info i ON i.user_id = u.user_id
WHERE u.sex = 'Male' and (i.image_type = 'primary' or i.image_type is null)
ORDER BY u.date_registered DESC LIMIT 6;
See here for more information.

Check if row has specific childs on join

I want to make a SQL to get the user which name is Mark and are the author of the posts with ids 1 and 3.
NOTE: It is unknown how many posts I need to check for. So it might need to generate that part of the SQL query using PHP.
How can that be done?
Users Table:
+----+----------+
| id | name |
+----+----------+
| 1 | Mark |
| 2 | John Doe |
+----+----------+
Posts Table
+----+-------------+-------------+
| id | text | author_id |
+----+-------------+-------------+
| 1 | First Post | 1 |
| 2 | Second Post | 2 |
| 3 | Last Post | 1 |
+----+-------------+-------------+
This is just a sample case of use, not real data.
NOTE: I know how to check if user is author on one post, but not multiple in the same row. So basicly that is what I need help with, I guess it must be a left join.
For making the check for the user named Mark and check if he is author for post id 1 I do the following:
SELECT users.*
FROM users
INNER JOIN posts
ON users.id = posts.author_id
WHERE
users.name = 'Mark'
&&
posts.author_id` = 1
I just selected the id from users. If you need more columns then just add it to the select and the group by clause.
SELECT users.id
FROM users
INNER JOIN posts ON users.id = posts.author_id
WHERE users.name = 'Mark'
AND posts.author_id in (1,3)
GROUP BY users.id
HAVING count(distinct posts.author_id) = 2
Use a sub-query to find only users with both 1 and 3:
SELECT users.*
FROM users
WHERE users.name = 'Mark'
and 2 = (select count(distinct posts.id)
where users.id = posts.author_id
and posts.id IN (1,3))
SELECT users.name, posts.posts, posts.authorid
FROM users INNER JOIN posts ON users.id = posts.authorid where posts.authorid = 1
You need to use HAVING clause to achieve desired outcome:
SELECT users.name
FROM users
INNER JOIN posts
on users.id = posts.author_id
WHERE users.name = 'Mark'
GROUP BY users.name
HAVING COUNT(posts.author_id) > 1

Categories