I have this table in my MySQL database:
I need a query, but I have a logical problem to "create" them.
I am User with userID "3".
I would like to get all userIDs, which have the same chatToken like me, but without me in the result.
The result should be: userID "1" and "2".
How can I do this?
According to your sample data, userID 3 appears twice and has two different chatTokens. So you want any user that has any one of your chatTokens but is not you (i.e. whose userID is not 3)
select userID
from table
where chatToken in (select chatToken
from table
where userID = 3)
and userId != 3
Related
I'm stuck on creating MySQL queries. I have two tables, which are user and factory. The data and structure are as below:
table user
enter image description here
table factory
enter image description here
From table user, u can see example, id = 1 got factoryID = 2,3. At table factory, id = 2 is F1 and id = 3 is F2.
Now, how i want to join the table, and display the data like example,
user.id = 1
user.name = Amira
user.factoryID = 2,3
factory.factoryName = F1,F2
Can anyone know how to write the query?
I suggest fixing your table design. Using FIND_IN_SET can do the trick, but you will be facing some performance issues, especially for larger data.
As per the question you could use:
select user.id,name,factoryID,group_concat(factoryName) as factoryName
from user
inner join factory ON FIND_IN_SET(factory.id,user.factoryID)
group by user.id,name,factoryID;
Result:
id name factoryID factoryName
1 Armira 2,3 F1,F2
2 Balqis 4,5 F3,F4
Demo
I have a table called followers and I want to be able to find the current users followers, display that list, and then compare those values against another user ID to see if they are following them or not from that same table
This is the table I'm using currently to get the list of followers for a specific user:
followers
-------
followId - primary key, the unique id for the follow relationship
userId - the user that is following someone
orgId - that someone that the user is following
I tried using a union query but I wouldn't want to due to performance reasons (table may contain high volume of records) and because it isn't scalable (I think)?
The expected output should be the list of orgId's for which the user(the user I am checking against) is following, and another column that shows whether my user(my userId that I provide) is following that orgId value (i.e a following column).
Hmmm, if I understand correctly, you have two users and you want to know which orgs followed by the first are also followed by the second:
select f.orgid,
(exists (select 1
from followers f2
where f2.userId = $seconduserid and
f2.orgid = f.orgid
)
) as seconduserflag
from followers f
where f.userId = $firstuserid
There are 3 tables needed to create this report. Two are tables with the data, and the 3rd table is one used in previous steps to gather the report data.
Table 1. Users.
us_usid (int)
us_user (varchar) default empty string
Table 2. UserGroups
ug_usid (int) = users.us_usid
ug_group (varchar 6) = group id
Table 3. Report
re_group (varchar 6) = usergroups.ug_group
re_registered_count (int) = count of users with username
Must simplifed schema, but shows the columns involved. The report table has around 500 group IDs in them. I need to count the users whose us_user <> '' and whos us_usid is in the ug_usid grouped by the ug_group IDs in the report table.
For example, the report table has '533103' as a group ID. The UserGroups table has 545 users who have that ug_group. Those ug_usids correspond to 545 users in the users table of which 373 have a value for the us_user string. I need to get that "373" number into the report table.
If figured out how to pull all of the other necessary data but cannot find a working (and efficient way- these are groups with between 1000 and 100000 members) method to figure out how many are registered in one fell swoop.
Count of users per user group, where there is a value in us_user
select ug.us_user, count(u.us_usid)
from UserGroups ug
inner join Usersu on ug.ug_usid = u.us_usid
where u.us_user IS NOT NULL
I use the following SQL query to get two random rows.
entry.sql
SELECT id, userID, img, points, votes
FROM entry
WHERE rotation = 1
ORDER BY RAND()
LIMIT 2
Now I would like to filter the possible rows. I do this here by rotation but I want more.
I have a second and a third table which look like this:
users.sql
ID | username
voted.sql
ID | userID | entryID | timestamp
I only want to get rows which the user hasn´t already voted on. So we should get all entryIDs for the user from voted.sql and make sure that the query above doesn´t pick them, how can I do this ?
Or would you save the data, in a different way, to make the already voted check easier ?
Use a join or subquery to do this, like:
SELECT id, user.userID, entry.img, entry.points, entry.votes
FROM entry
WHERE userID NOT IN (SELECT DISTINCT userID
FROM voted
WHERE userID = entry.userID);
Say I have a subscribers table, that has a row for each user like this..
id name subscribers
1 user1 user2,user3,user4
2 user2 user4,user5,user3
3 user3 user1,user6,user2 etc...
What I want to do is, run a select statement like this..
SELECT subscribers from table where id = '1'
.. And then, limit how many subscribers to show me
i.e. If I limited it to 2, it would only SELECT "user2,user3" from table.subscribers WHERE id=1
I know I can limit it after selecting all with PHP but I don't want to run into performance problems, if there were millions of usernames in each column...
Also, is this the best structure to set up a subscibe/follow system.. Or is there a better way?
You are storing multiple values in the same field. This is bad!
You need a second table, to represent subscriptions - it would have a column userid and subscriberuserid (or something similar).
For every subscriber that a user has, there would be a record in this table with that user's userid (and the userid of their subscriber).
Then, you can limit to your hearts content:
SELECT subscribers.subscriberuserid
FROM subscribers
WHERE userid = 1
LIMIT 2
I think Many-To-Many relation would be more convenient, you could then easily limit your record numbers...