I am trying to join my users table with another table using the following query...
SELECT * FROM (`activities`)
JOIN `users` ON `users`.`id` = `activities`.`user`
WHERE `user_subdomain` = 'hi' OR user_subdomain = ''
ORDER BY `activities`.`id` desc
LIMIT 10
Is there any way to do the join so that the id of the user does not replace the id of the activity?
For example, currently if there is an activity with the id of 10 and the user 2 the id will be replaced by the id of the users table and show as 2 after I run the query.
Thanks a lot for the help!
Whenever you are joining tables, you ought to be explicit about the columns you select rather than using SELECT *, and specify column aliases for them when the same column name is used in multiple tables.
SELECT
activities.id,
activities.othercol,
/* Alias to userid */
users.id AS userid,
users.name,
users.anothercolumn
FROM (`activities`)
JOIN `users` ON `users`.`id` = `activities`.`user`
WHERE `user_subdomain` = 'hi' OR user_subdomain = ''
ORDER BY `activities`.`id` desc
LIMIT 10
Though it isn't strictly necessary to prepend the table name to each, unless the column names are the same.
SELECT
activities.id AS activityid,
othercol,
users.id AS userid,
name,
anothercolumn
Related
My tables
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY id DESC
LIMIT 1";
This is my SQL query, my task is to show the last records from 3 tables, but the table is blank, I don't know why,thanks in advance people :)
I guess the problem is coming from the ORDER BY id DESC .
Indeed, you have no column so called id.
You should probably remove this clause, in order to make your code work.
If you want to take the last records anyway, you can put an ORDER BY address_id DESC which will do the job !
The code directly edited :
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY adress_id DESC
LIMIT 1";
This may work:
SELECT a.address_id, u.user_id, n.note_id
FROM addresses a
LEFT JOIN users_addresses ua ON ua.ua_address_id = a.address_id
LEFT JOIN users u ON u.user_id = ua.ua_user_id
LEFT JOIN notes n ON n.note_user_id = u.user_id
ORDER BY a.address_id DESC
LIMIT 1
Here is the query to get all data from all the tables, not sure what do you mean last records from 3 tables, I can see four tables there:
SELECT *
FROM `addresses`
LEFT JOIN `users_addresses` ON `users_addresses`.`ua_address_id` = `addresses`.`address_id`
LEFT JOIN `users` ON `users`.`user_id` = `users_addresses`.`ua_user_id`
LEFT JOIN `notes` ON `notes`.`note_user_id` = `users`.`user_id`;
I'm using Sqlsrv and I need to find common values within the same table using PHP. There are two main columns, user_id and bssid.
Using a selected $user_id, I need to find all common "bssid" values and return a set of users which contain these values (this is if another user_id has one or more bssid the same).
This is my query string so far. My logic doesn't work.
$sql = "SELECT A.* FROM AccessPoints A INNER JOIN (SELECT bssid, user_id
FROM AccessPoints WHERE user_id='$userID'
GROUP BY bssid, user_id
HAVING COUNT(*) >= 1) B
ON A.bssid = B.bssidB.user_id = A.user_id";
The database schema is as such:
A sample of the dataset can be seen as such, and contains the data from two users:
I simply want to query a user_id and return all other user_ids which have similar bssids
If you want all the user_id including the the one you are searching on then:
SELECT user_id
FROM AccessPoints
WHERE bssid IN(SELECT bssid
FROM AccessPoints)
WHERE user_id = '$userID')
If you want to exclude the one you are searching from the output then:
SELECT user_id
FROM AccessPoints
WHERE bssid IN(SELECT bssid
FROM AccessPoints)
WHERE user_id = '$userID')
AND user_id != '$userID'
How can i select all the id's from a table, and let select all the songs with the id;s from the other table?
$query = "SELECT * FROM songs WHERE id = (SELECT songid FROM top10 order by id)'";
You can use IN for that (or EXISTS):
select *
from songs
where id in (
select songid
from top10 )
Based on your comments, you might actually be looking to use JOIN (just realize if there are duplicate records in the top10 table, this could return duplicate results):
select s.*
from songs s
join top10 t on s.id = t.songid
order by t.id
SQL Fiddle Demo
I want to create a sub-account system (PHP & MYSQL).
I have a user table (users) and a sub users table (sub_users).
How can check if the user is available in the user table, or in the sub users table?
My code:
SELECT DISTINCT *
FROM users
WHERE userid = "steven"
OR WHERE EXISTS (SELECT *
FROM sub_users
WHERE sub_users.userid = "steven");
ERROR: Check your syntax near "steven"
Also tried:
SELECT *
FROM users
LEFT JOIN sub_users
ON sub_users.user_userid = users.userid
WHERE users.userid = 'steven'
OR sub_users.userid = 'steven'
Same error.
In the first place, you only want one where clause. I would also use single quote instead of double quotes:
SELECT DISTINCT *
FROM users
WHERE userid = 'steven' or
EXISTS (SELECT *
FROM sub_users
WHERE sub_users.userid = 'steven'
);
I doubt you need the distinct keyword, if you are fetching all the columns from users.
Your second query looks ok. Are you sure you are not running the first query twice?
EDIT:
I'm trying to figure out what you want to return. The following returns 1 if 'steven' appears in either table and 0 otherwise:
select (case when exists (select 1 from users where users.userid = 'steven') and
exists (select 1 from sub_users where sub_users.userid = 'steven')
then 1
else 0
end);
This method saves on the overhead of a join and will readily take advantage of indexes on users(userid) and sub_users(userid).
You could also use a union:
(SELECT userid FROM users WHERE userid = 'steven')
UNION
(SELECT userid FROM sub_users WHERE userid = 'steven');
select * from
(select * from users) x,
(select * from sub_users) y
where x.user_id = 'steven' or y.user_id = 'steven'
Good luck !!!
I have tables like this
COUNT tbl
id, userid, linkid, count
4
1
6
LINKS tbl
id, linkname, linkurl
What i want to do is to order the 'linkname' column in order of the count column and put it in an array. I am struggling becasue i am not understanding how to use JOIN.
I need to get linkid WHERE userid = $userid
Try this:
$userid = intval( $userid ); // Hopefully it's already an integer,
// but protect yourself from SQL Injection
SELECT linkname, C.count FROM Links INNER JOIN `Count` C ON C.linkid = Links.id
WHERE userid = $userid
ORDER BY C.count ASC
Try this: SELECT * FROM Links L JOIN Count C ON L.id = C.linkid then you should have a count column.
Also, I recommend you don't use "count" as the name of a table since it is a SQL reserved word.