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 !!!
Related
I'm trying to make a homepage somewhat like Facebook, I made it so it could show the posts from the people I follow, but I couldn't see my own posts as I can't follow myself. Here is the line of SQL code I've written (it contains PHP variables):
SELECT *
FROM user_posts
INNER JOIN user_following ON user_posts.username = user_following.username
WHERE user_following.follower = '$me->username'
ORDER BY id DESC
LIMIT 0, 15
The user_posts table contains all the posts.
The user_following table contains all follow data, where username is the user being followed, and the follower is the user following the username
$me->username is the username of the user logged in.
user_posts table structure:
user_following table structure:
Thanks, in advance!
There's a couple of different ways to skin this query:
Sub-query
SELECT *
FROM user_posts
WHERE user_posts.username = 'bob'
OR user_posts.username IN(
SELECT username
FROM user_following
WHERE user_posts.username = user_following.username
)
LIMIT 0, 15
http://sqlfiddle.com/#!9/6bf2c6/9
Use the Users Table
Requires GROUP BY or DISTINCT user_posts.id, which are non-optimal.
SELECT
user_posts.*
FROM users
LEFT JOIN user_following ON users.username = user_following.username
INNER JOIN user_posts ON (
users.username = user_posts.username
OR user_following.follower = user_posts.username
)
WHERE users.username = 'bob'
GROUP BY user_posts.id
LIMIT 0, 15
http://sqlfiddle.com/#!9/d91be/1
IMPORTANT! Make sure and index those columns in your table. Otherwise, performance will suffer as the tables get bigger (especially user_following).
Try this code:
select *
from user_posts up
join user_following uf on up.username = uf.username
where uf.follower = '$me->username'
or up.username = '$me->username'
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
Im trying to construct a query that goes over 3 tables and im COMPLETELY stumped ... my knowledge limit is basic 1 table query and i need some help before i stick my head in a blender.
I have the following query
SELECT * FROM internalrole WHERE introle = $imarole
Im fine with that part .. its the next thats getting me all stressed.
That query returns the following columns ( id, user_id, introle, proven, used )
What i then need to do is take the user_id from the results returned and use it to get the following
SELECT * FROM users WHERE id = user_id(from previous query) AND archive = 0 and status = 8
I need to put that into 1 query, but wait, theres more .... from the results there, i need to check if that user's 'id' is in the availability table, if it is, check the date ( column name is date ) and if it matches todays date, dont return that one user.
I need to put all that in one query :S ... i have NO IDEA how to do it, thinking about it makes my head shake ... If someone could help me out, i would be eternaly grateful.
Cheers,
Use INNER JOIN, which links tables to each other based on a common attribute (typically a primary - foreign key relationship)
say an attribute, 'id', links table1 and table2
SELECT t1.att1, t2.att2
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id --essentially, this links ids that are equal with each other together to make one large table row
To add more tables, just add more join clauses.
SELECT u.*
FROM internalrole ir
INNER JOIN users u
ON ir.user_id = u.id
AND u.archive = 0
AND u.status = 8
LEFT JOIN availability a
ON ir.user_id = a.user_id
AND a.date = CURDATE()
WHERE ir.introle = $imarole
AND a.user_id IS NULL /* User does NOT exist in availability table w/ today's date */
EDIT: This second query is based on the comments below, asking to show only users who do exist in the availability table.
SELECT u.*
FROM internalrole ir
INNER JOIN users u
ON ir.user_id = u.id
AND u.archive = 0
AND u.status = 8
INNER JOIN availability a
ON ir.user_id = a.user_id
WHERE ir.introle = $imarole
Hmm, maybe something like this
SELECT * FROM users WHERE id IN (SELECT user_id FROM internalrole WHERE introle = $imarole) AND archive = 0 and status = 8;
A handy thing for me to remember is that tables are essentially arrays in SQL.
HTH!
Nested queries are your friend.
SELECT * FROM users WHERE id in (SELECT user_id FROM internalrole WHERE introle = $imarole) AND archive = 0 and status = 8
Alternatively joins:
SELECT * FROM users INNER JOIN internalrole ON users.id = internalrole.user_id WHERE internalrole.user_id = $imarole AND users.archive = 0 and users.status = 8
SELECT * from User returns 75 users. Is it possible to select 1st user, and 75th user without doing while($row = mysql_fetch_assoc($result)) ?? and how?
UPDATE
Just to be more clear: I need to have SELECT * as I need the first and 75th user before I do while mysql_fetch_assoc so ASC, DESC, LIMIT answers not required.
SELECT * from User LIMIT 1
UNION
SELECT * from User LIMIT 74,1
Edit
#Kay: PHP can't change the internal order of the resultset after it's created.
If the query always returns 75 rows then the only way to access the 1st and the 75th before anything else would be to use mysql_data_seek which moves the internal result pointer:
$result = mysql_query('SELECT * from User');
mysql_data_seek($result, 1);
$row1 = mysql_fetch_assoc($result);
mysql_data_seek($result, 75);
$row75 = mysql_fetch_assoc($result);
Note that if the above is followed by a while, the pointer must be reset to a suitable position.
If you can sort it, you can.
Select * from User order by [something] asc limit 1
and
Select * from User order by [something] desc limit 1
Assuming you have 'id' as a primary key and you need the last one (not the 75th one) you could try something like:
SELECT * FROM User WHERE id IN ((SELECT min(id) FROM user b), (SELECT max(id) FROM user c))
SELECT
(SELECT column FROM table WHERE [condition] ORDER BY column LIMIT 1) as 'first',
(SELECT column FROM table WHERE [condition] ORDER BY column DESC LIMIT 1) as 'last'
SELECT u.*
FROM Users u
INNER JOIN (
SELECT MIN(UserID) AS UserID FROM Users
UNION ALL SELECT MAX(UserID) FROM Users
) um ON um.UserID = u.UserID
Edit
I'm not sure I completely understand what you need but following gets the first and last user followed by everyone else.
SELECT um.SortOrder, u.*
FROM Users u
INNER JOIN (
SELECT 1 AS SortOrder, MIN(UserID) AS UserID FROM Users
UNION ALL SELECT 2, MAX(UserID) FROM Users
) um ON um.UserID = u.UserID
UNION ALL
SELECT 3 AS SortOrder, u.*
FROM Users u
LEFT OUTER JOIN (
SELECT MIN(UserID) AS UserID FROM Users
UNION ALL SELECT MAX(UserID) FROM Users
) um ON um.UserID = u.UserID
WHERE um.UserID IS NULL
ORDER BY
SortOrder
Well, the title of your question is a bit different from the explanation you gave.
I say so cos if you want to select first and last row, it might be different to select 1st and 75th row cos in case the rows increase or reduce, the last might not be the 75th row.
To answer the part of the first and last row, i think you can do this.
select distinct(id) from users where id in ((select max(id) from user), (select min(id) from users));
This query will work well in the hope that users are ordered by id.
But if you are talking about the 1st and 75th row, then i'll settle with #Saul's answer .
Hope this helps.
How do I specify the parent query field from within a subquery in MySQL?
For Example:
I have written a basic Bulletin Board type program in PHP.
In the database each post contains: id(PK) and parent_id(the id of the parent post). If the post is itself a parent, then its parent_id is set to 0.
I am trying to write a mySQL query that will find every parent post and the number of children that the parent has.
$query = "SELECT id, (
SELECT COUNT(1)
FROM post_table
WHERE parent_id = id
) as num_children
FROM post_table
WHERE parent_id = 0";
The tricky part is that the first id doesn't know that it should be referring to the second id that is outside of the subquery. I know that I can do SELECT id AS id_tmp and then refer to it inside the subquery, but then if I want to also return the id and keep "id" as the column name, then I'd have to do a query that returns me 2 columns with the same data (which seems messy to me)
$query = "SELECT id, id AS id_tmp,
(SELECT COUNT(1)
FROM post_table
WHERE parent_id = id_tmp) as num_children
FROM post_table
WHERE parent_id = 0";
The messy way works fine, but I feel an opportunity to learn something here so I thought I'd post the question.
How about:
$query = "SELECT p1.id,
(SELECT COUNT(1)
FROM post_table p2
WHERE p2.parent_id = p1.id) as num_children
FROM post_table p1
WHERE p1.parent_id = 0";
or if you put an alias on the p1.id, you might say:
$query = "SELECT p1.id as p1_id,
(SELECT COUNT(1)
FROM post_table p2
WHERE p2.parent_id = p1.id) as num_children
FROM post_table p1
WHERE p1.parent_id = 0";
You could try something like this
SELECT pt.id,
CountTable.Cnt
FROM post_table pt LEFT JOIN
(
SELECT parent_id,
COUNT(1) Cnt
FROM post_table
WHERE parent_id <> 0
GROUP BY parent_id
) CountTable ON pt.id = CountTable.parent_id
WHERE pt.parent_id = 0
To get back to your example, use the alias of the main table in the sub select
SELECT pt.id,
(SELECT COUNT(1) FROM post_table WHERE parent_id = pt.id)
FROM post_table pt
WHERE pt.parent_id = 0
Give the tables unique names:
$query = "SELECT a.id, (SELECT COUNT(1) FROM post_table b WHERE parent_id = a.id) as num_children FROM post_table a WHERE a.parent_id = 0";
The following syntax works in Oracle. Can you test if the same works in MYSQL too?
It is called scalar subquery in Oracle.
You would just need to alias the two tables differently to distinguish between them if you are using the same table twice.
sql> select empno,
2 (select dname from dept where deptno = emp.deptno) dname
3 from emp
4 where empno = 7369;
EMPNO DNAME
---------- --------------
7369 RESEARCH
sql> select parent.empno,
2 (select mgr from emp where empno = parent.empno) mgr
3 from emp parent
4 where empno = 7876;
EMPNO MGR
---------- ----------
7876 7788
Thanks Don. I had a nested query as shown below and a WHERE clause in it wasn't able to determine alias v1. Here is the code which isn't working:
Select
teamid,
teamname
FROM
team as t1
INNER JOIN (
SELECT
venue_id,
venue_scores,
venue_name
FROM venue
WHERE venue_scores = (
SELECT
MAX(venue_scores)
FROM venue as v2
WHERE v2.venue_id = v1.venue_id /* this where clause wasn't working */
) as v1 /* v1 alias already present here */
);
So, I just added the alias v1 again inside the JOIN. Which made it work.
Select
teamid,
teamname
FROM
team as t1
INNER JOIN (
SELECT
venue_id,
venue_scores,
venue_name
FROM venue as v1 /* added alias v1 here again */
WHERE venue_scores = (
SELECT
MAX(venue_scores)
FROM venue as v2
WHERE v2.venue_id = v1.venue_id /* Now this works!! */
) as v1 /* v1 alias already present here */
);
Hope this will be helpful for someone.
Parent query field within a subquery in MySQL 8.
I'm selecing games scores on the basis of username from tblgamescores using nested query.
SELECT
GameScoresID,
(SELECT Username FROM tblaccounts WHERE AccountID = FromAccountID) AS FromUsername,
(SELECT Username FROM tblaccounts WHERE AccountID = ToAccountID) AS ToUsername,
(SELECT Username FROM tblaccounts WHERE AccountID = WinAccountID) AS WinUsername,
(SELECT Username FROM tblaccounts WHERE AccountID = LossAccountID) AS LossUsername,
FromUserScore,
ToUserScore
FROM tblgamescores a
WHERE FromAccountID = (SELECT AccountID FROM tblaccounts WHERE Username = "MHamzaRajput");