EDIT by:lawrence.
I got the right query now
select *
from users, friends
where (users.id=friends.user_id1 and friends.user_id2=$profileID) or (users.id=friends.user_id2 and friends.user_id1=$profileID)
Question answered
I need some help joining results from my friends and users table
This is what my friends table look like
id user_id1 user_id2
1 | 2 | 3
1 | 2 | 4
1 | 2 | 5
1 | 6 | 2
Users table
id name
2 | sarah
3 | emma
4 | lawrence
5 | cynthia
6 | suzie
I could easily just have two rows for each relation and do a simple query.
But i prefer having one row per relation,
So lets assume that we are watching page member.php?profile=2
and there is a list of friends, what does the query look like.
This works fine if i have two rows per relation but i dont want that....
SELECT * FROM friends, users WHERE friends.user_id1 = $profileID AND friends.user_id2 = users.id ORDER BY friends.id DESC LIMIT 16
Do you get me? something along like
SELECT * FROM friends,users WHERE friends.user_id1 = $profileID AND ALSO WHERE friends.user_id2 = $profileID AND THEN GET FROM users WHERE users.id = friends.user_id1 AND ALSO WHERE users.id = friends.user_id2
I hope I made myself clear
I'm not sure i understand your question but won't this do?
SELECT * FROM friends, users where friends.user_id1 = $profileID or friends.userid2 = $profileID and users.id = friends.user_id1 or users.id = friends.user_id2
You want a left join (using the LEFT JOIN operator), not a cartesian join (using the FROM table1, table2 syntax).
http://www.w3schools.com/sql/sql_join_left.asp
Tip: With your cross-reference table instead of having an id column you can create a compound key.
Related
I have two tables like these:
Table "users":
user_id | source
----------------
1 | 2
2 | 2
3 | 3
4 | 0
Table "sources":
source_id | name
----------------
1 | "one"
2 | "two"
3 | "three"
4 | "four"
Now I need to SELECT (*) FROM source and additionally COUNT "users" that have this source, BUT if there is an additional filter(requests by PHP mysqli), then additionally sort "sources" table by its users count.
What is the best way to do so, and is it possible to do in one statement?
--------------Added editing----------
The first part(SELECT with count from another table) I'm doing this way:
SELECT
id, name
(select count(*) from users where source = sources.id) as sourceUsersCount
FROM sources
And now, how to order this list by users count in each source?
Please check the below query if this is what you need.
select s.*,a.c from sources s
left join
(select count(*) as c,source as src
from user u join sources s
on s.source_id = u.source group by u.source) a
on s.source_id = a.src;
Count the number of users:
SELECT sources.*, COUNT(users.user_id) FROM sources
LEFT JOIN users ON users.source_id = sources.source_id
GROUP BY sources.source_id;
I assume by filters you mean the WHERE clause:
SELECT sources.*, COUNT(users.user_id) FROM sources
LEFT JOIN users ON users.source_id = sources.source_id
WHERE sources.source_id = 2
GROUP BY sources.source_id;
And you can always attach an ORDER BY on the end for sorting:
SELECT sources.*, COUNT(users.user_id) FROM sources
LEFT JOIN users ON users.source_id = sources.source_id
GROUP BY sources.source_id
ORDER BY sources.source_id DESC;
Achieved it by doing so:
SELECT
sources.*,
count(users.source) as sourceUsersCount
FROM sources
LEFT JOIN users ON sources.id = users.source
//In case of additional filters
WHERE
id != 0 AND (name LIKE %?% OR id LIKE %?%)
//\\
GROUP BY sources.id
//In case of sorting by "users" count
ORDER BY sourceUsersCount ASC
//\\
Is it the best way, or maybe there are some faster variants?
I'm really unsure how best to go about writing this query. I have 3 tables and I need to run a query pulling data from one, based on conditions in the others.
Tables: surveys, survey_countries, survey_categories
surveys:
id | survey_id | network_id
survey_countries:
id | survey_id | network_id | country
survey_categories:
id | survey_id | network_id | category
(The survey_id in survey_countries and survey_categories relates to the survey_id column in the surveys table as opposed to the id column).
I need to retrieve data from surveys for a specific country and a specific category. Then I need to be able to UNION for other categories, but I guess I can do that later. My attempt:
SELECT surveys.*
FROM survey_countries
LEFT JOIN surveys ON survey_countries.survey_id = surveys.survey_id ANd survey_countries.network_id = surveys.network_id
LEFT JOIN survey_categories ON survey_countries.survey_id = surveys.survey_id AND survey_categories.network_id = surveys.network_id
WHERE survey_countries.country = 'GB'
AND survey_categories.category = 'my_category'
GROUP BY surveys.id
Thanks!
EDIT: the following seems to work:
SELECT s.*, ca.category, co.country
FROM surveys s
LEFT JOIN survey_countries co ON s.survey_id = co.survey_id AND s.network_id = co.network_id
LEFT JOIN survey_categories ca ON s.survey_id = ca.survey_id AND s.network_id = ca.network_id
WHERE ca.category = 'uncategorised'
AND co.country = 'GB';
I'm just not sure it's the best way to do it since I need to grab surveys with multiple categories later on?
Without example data and an example output, helping you on this query is very difficult. However..... your 2nd join relates survey_countries with surveys. I think you mean to join it to the survey_categories. Also, you have a Group By, with no aggregate functions. Get rid of it until you have too much information an need to summarize columns
I am not saying this is correct, but it is slightly more 'correct' than yours
SELECT surveys.*
FROM survey_countries
LEFT JOIN surveys
ON survey_countries.survey_id = surveys.survey_id
AND survey_countries.network_id = surveys.network_id
LEFT JOIN survey_categories
ON survey_categories.survey_id = surveys.survey_id
AND survey_categories.network_id = surveys.network_id
WHERE survey_countries.country = 'GB'
AND survey_categories.category = 'my_category'
(If you can, load up a schema and sample data in sqlfiddle. It will make this problem easier to solve.)
I'm working with PHP and PDO, and I need to recolect information joining 3 tables:
photos
albums
album_photos
The table have the following structure:
photos:
photo_id (int)
path (varchar)
nick (varchar)
date (timestamp)
albums
album_id (int)
album_name (varchar)
nick (varchar)
date (timestamp)
album_photos
album_id (int)
photo_id (int)
nick (varchar)
date (timestamp)
So, I want to show all the albums with a max of 5 photos for each one, where the user nick is 'owner'.
To be shown as follows:
album_name_1:
[photo_1]
[photo_2]
[photo_3]
[photo_4]
[photo_5]
album_name_2:
[photo_1]
[photo_2]
[photo_3]
[photo_4]
[photo_5]
I only know that something like this can be made with INNER JOIN, but I can't found how I can make it with 3 tables.
There examples or other help that I can get to do this?
SELECT a.*, c.date
FROM Album a
INNER JOIN Album_Photo b
ON a.Album_ID = b.Album_ID
INNER JOIN Photo c
ON b.Photo_ID = c.Photo_ID
WHERE c.Nick = 'owner' AND
(
SELECT COUNT(*)
FROM album_photo d
WHERE b.album_id = d.album_id AND
d.nick = 'owner' AND
b.date >= d.date
) <= 2 // <<== change this value to 5
SQLFiddle Demo
I think this can be resolved using Stored Procedures. Using variables and loops, you can retrieve the records you desire. Getting only a maximum of 5 records from an album is quite challenging when you only use the basic SQL commands. Sometimes, you cannot derive the right records. I suggest you use Stored Proc. :)
Try like
"SELECT albums.*,photos.id,photos.path,photo_date as Pht_date
FROM albums
JOIN album_photos
ON album_photos.album_id = albums.album_id
JOIN photos
ON photos.photos_id = album_photos_id
"
But you cont give individual limit for photos where it involved in "JOIN" orelse you need to give individually like
$album_ids = "SELECT album_id FROM albums";
then for the photos you need to write query liks
"SELECT photos.* FROM photos
WHERE album_photos.album_id in (".$album_ids.") AND album_photos.nick = 'owner'
JOIN album_photos
ON album_photos.photo_id = photos.photos_id
LIMIT 0,10
"
So lets say i have 2 tables.
table users:
id | name | date
table weight_tracking;
id | user_id | previous_weight | current_weight | date
weight_tracking table is being updated daily with user current weight and previous_weight.
I am trying to display all users ordering by previous_weight - current_weight (so by the difference in weight - who ever lost the most weight will show up first)
Can that be done with 1 call ?
SELECT users.*, weight_tracking.*
FROM users left join weight_tracking on users.id = weight_tracking.user_id
ORDER BY previous_weight - current_weight desc
if a user can have more than one row in weight_tracking table, you could use something like this:
SELECT users.*
FROM users left join weight_tracking on users.id = weight_tracking.user_id
GROUP BY users.id
ORDER BY max(previous_weight) - min(current_weight) desc
Ok, I have this first table which has, among other things:
table 1: id | depID (every id has one depID)
Then, I have a second table where I have table 2: userID | depID (where an userID is associated with multiple depIDs in separate rows. Also, I have table 3 with userID | rankID (where an userID is associated with one rankID).
I need to get all id and depID from table 1, and then to check, which userIDs of table 2 shares the same depID (table1.depID = table2.depID), and then, to check which of those userIDs from table 2 has rankID = $rID
Thanks guys.
I think this SQL should get you what you want, but I'm not 100% clear from the wording of the question:
SELECT table2.userID
FROM table1
JOIN table2
ON table1.depID = table2.depID
JOIN table3
ON table2.userID = table3.userID
AND table3.rankID = $rID;