This question already has answers here:
SELECTING with multiple WHERE conditions on same column
(12 answers)
Closed 1 year ago.
I don't know if the question title is so clear, but here is my question:
I had table UsersMovements which contains Users along with their movements
UsersMovements:
ID
UserID
MovementID
Comments
I need help looking for a query which would give me if users 1, 2 & 3 had been in a common MovementID, knowing that I had know what is the MovementID
The real case is that I want to see if those X users which I would select been in an area (in a limited interval, assuming I had date/Time in the table)
Thank you
The following returns all movement ids that have all three users:
select movementid
from usermovements
where userid in (1, 2, 3)
group by movementid
having count(distinct userid) = 3
Related
This question already has answers here:
mysql count function
(3 answers)
Closed 6 months ago.
I have a referral system, email is the referral code it's working but I want to count how many times a value (email) appears in a row as to display to the user how many person he/she have referred.
Use COUNT() function which shows the number of records returned by a select query
select email, IFNULL(COUNT(id),0)total_ referred from table where email = 'example#gmail.com';
Your query needs to implement COUNT(), which will give you the total number that this email string was used:
SELECT COUNT(email) FROM table_name WHERE email = 'xxx#gmail.com';
or if it is by numeric id:
SELECT COUNT(email_id) FROM table_name WHERE email_id = 111;
This question already has answers here:
SQL query to count number of times certain values occur in multiple rows
(5 answers)
Closed 4 years ago.
SELECT firstname, lastname, id
FROM contact
WHERE id IN
(SELECT contactid
FROM taglist
WHERE contactid IN
(SELECT contactid
FROM customerlist
WHERE companyid = 1) AND (tagid = 1 OR tagid = 4))
I have the query above where there is a single table that has a userid and a tag id. Each user can have multiple tags that are listed as separate entries on this table.
I need to be able to select only users with two or more specified tags. I thought the above query would do this, but it is returning every user with either tag 1 or tag 4.
If I change it to this: (tagid = 1 AND tagid = 4)) I get 0 results because it is only looking at each individual entry, and no entry has more than one tag per user.
This example only has two tags selected, but the goal here is to allow the end user to be able to select any number of available tags and only show results where the user matches all selected tags.
I don't have any way to know a specific number of tags that are going to be selected to match on. Currently there are 20 available tags, and clients can add their own tags if they wish.
So how do I accomplish this? I am very new to SQL queries, and not very familiar with joins yet so I am thinking this is going to have something to do with it, but I don't know. Any nudges in the right direction would be greatly appreciated.
You can get contacts with the two tags by doing:
SELECT contactid
FROM taglist
WHERE tagid IN (1, 4)
GROUP BY contactid
HAVING COUNT(*) = 2;
The rest of your query is doing other stuff not mentioned in your question, so it is unclear what you really want. Obviously, you can use this as a subquery to get full contact information.
This question already has answers here:
MySQL: multiple tables or one table with many columns? [closed]
(8 answers)
Closed 5 years ago.
This is my first time doing a bit of medium size project with laravel. I have an authentication system based on Sentinel package. As for my project requirements my users table became filled with so many columns like: first name, last name, email , address , country_code , description, rating, phone_number, company_name , and so on
What should I do? which is better? keep them all in one table? or separate this table into many tables?
Please when you answer me tell me the logic behind the answer you gave me.
why do you want to separate your table?
if you have many table , when you want select from your table , you need more join and this makes bad performance for your app.
This question already has answers here:
SQL query return data from multiple tables
(6 answers)
Closed 5 years ago.
Am trying to retrieve data from 3 tables at once but i am not sure how to do it in mysql (to be truthful i am really bad at database stuff so this confuse the hell out me).
1st logins table that has 3 columns id, email, password
2nd users table that has 4 columns id, name, surname, location_id
3rd locations has 3 columns location_id, country, city
what i want to do is write query using the users id to get the email from the logins table, then get name and surname from the users table. I then want to use the location_id i got from the users table to find out what city he or she is from.
i hope that makes sense to you and thanks for your time.
maybe this could be helpful
select a.email, B.name, B.surname, C.city
from logings A join users B on (A.id=B.id) join locations C on
This question already has answers here:
Query with multiple values in a column
(4 answers)
Closed 6 years ago.
Hi I'm not sure how to even formulate this question but I'll try my best.
I have a table called games, in this table there's 3 columns, ID (INT), epoch (INT) and users (TEXT), now in each users column there is values from 4 to 25 values separated by a comma, the values are from 1 to 6 characters, so for instance:
ID, EPOCH, USERS
1, 1461779167, (123, 58234, 548245, 225122)
2, 1461774823, (326784, 54235, 6373, 3566, 384174)
3, 1461773423, (326784, 542355, 234, 351)
Now my problem is I need to fetech only ONE among the users let's say I need to fetch the "user" 54235, it cannot be CONTAINS because it would collide with 542355 so what should I do? How should I properly arrange these columns to avoid such situations or to simplify it in general?
A "proper arrangement" of these columns requires some normalization.
You could have a second table, called something like "player_games", which consists of:
Columns id, game, player
Then you associate a user with a game by inserting into player_games.
So if player 2 is associated with game 5, you do:
INSERT INTO player_games (game, player) VALUES (5, 2);
And then, if you need to get all games associated with player 2:
SELECT game FROM player_games WHERE player = 2;
And if you need to get all players associated with game 5:
SELECT player FROM player_games WHERE game = 5;
simplest hack you can do is instead of matching "54235" you can match "54235,". match comma as well and it will do the trick.
make sure you columns comma arrangement is proper atleast.
but this is not the right way to store results. you should have different table for mapping.