How do I make this select query? - php

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.

Related

Mysql query returns duplicate rows when joining tables

I have a table that stores user_avatar filepath :
id | userid | file | last_update |
1 | 23 | myphoto.png | x_timestamp |
2 | 23 | myphoto2.png | x_timestamp |
3 | 25 | myavatar.png | x_timestamp |
I have a table that returns user_score :
id | gamerid | score | last_update |
1 | 23 | 44 | x_timestamp |
2 | 25 | 99 | x_timestamp |
This is the query I use to display user scores with their avatar
SELECT * FROM user_score us LEFT JOIN user_avatar ua ON us.gamerid=ua.userid
Above query will return 2 duplicate rows for user 23 because he as downloaded 2 different photos in the user_avatar table. Right now it outputs like this:
GAMER SCORES RESULTS
myphoto.png | 44
myphoto2.png | 44
myavatar.png | 99
In reality, only one row should be returned for each user and the last avatar downloaded should be used as primary avatar. How can I fix this?
You can try below -
SELECT * FROM user_score us LEFT JOIN user_avatar ua ON us.gamerid=ua.userid
and ua.last_update =
(select max(last_update) from user_avatar ua1 where ua.userid=ua1.userid)
You should select the value for the most recent avatar .. probably related ton the max id for user_id
SELECT ua.*, us.*
FROM user_score us
INNER JOIN (
select user_id, max(id) max_id
from user_avatar
) t on t.user_id = us.user_id
LEFT JOIN user_avatar ua
ON us.gamerid=ua.u serid
AND t.max_id = ua.id
In reality, the last avatar downloa

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 where left join syntax

I have a problem. I have 2 database tables.
table 1 people:
+----------+--------------+
| id | name |
+----------+--------------+
| 1 | johanalj |
| 2 | hjgjhggjh |
+----------+--------------+
table 2 images of people:
+----------+--------------+----------------+
| id | url | people_ID |
+----------+--------------+----------------+
| 1 | 3765345.png | 1 |
| 2 | 87e58974.png | 1 |
+----------+--------------+----------------+
Now I want to select person with id 1 from table 1 and all pictures from table 2 that have people_ID 1.
I tried LEFT JOIN in combination with a WHERE but cant get it to work
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE id = '1'";
But I get a no result massage. What am I doing wrong?
There is an error(ambiguous column id). Both tables have id column. You need to add the table alias with id. try with -
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE p.id = '1'";

MYSQL get row rank

I have a mysql table and I need to get random row and get the rank of total view
+--------+------------+---------+
| id | name |totalview|
+--------+------------+---------+
| 1 | ex1 | 20 |
| 2 | ex2 | 100 |
| 3 | ex3 | 30 |
| 4 | ex4 | 40 |
+--------+------------+---------+
for example :
SELECT * FROM `table` WHERE `id` = '$rand';
$rand may be 1 or 2 etc ..
I need to get rank of this row by totalview
thank's
SELECT *,
(SELECT COUNT(*) FROM table t2 WHERE totalview > t1.totalview ) + 1 cnt
FROM table t1
WHERE id = '$rand';
SELECT SUM(ref.totalview < t.totalview) FROM t1 CROSS JOIN t1 ref WHERE t1.id = '$rand'

SELECT multiple table

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' ";

Categories