mysql compare two tables - mysql

I have a mysql database that has 2 table. the first table contains the user information, and the second table contains the votes. there is a common field between the two (userid).
but after checking the num rows in each table I found that the first table contains nearly 1000 users more than the second, so there are almost 1000 members whio didn't vote.
I want to query the two tables and get an array containing the userid of the members who didn't vote.
How to?
Thanks.

You need to join both tables and filter out which users don't have corresponding record in votes table.
SELECT id FROM members
LEFT JOIN votes ON userid=id
WHERE votes.userid IS NULL

Related

MySQL count registered users joined across multiple tables

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

MySQL - Select multiple rows with the same values with one having to be different

I have a table for IP addresses used and the associated userId. Now I know how I would select multiple entries of the same IP but how would I go about only selecting multiple entries WITH a different userId?
You can use conditional aggregation over each ip address and check to see if more than one user be associated with that ip address.
SELECT ip
FROM yourTable
GROUP BY ip
HAVING COUNT(DISTINCT userid) > 1 -- or a higher number if you want
you can use sql join do it for you
INNER JOIN gets all records from one table that have some related entry in a second table
LEFT JOIN gets all records from the LEFT linked table but if you have selected some columns from the RIGHT table, if there is no related records, these columns will contain NULL
RIGHT JOIN is like the above but gets all records in the RIGHT table
FULL JOIN gets all records from both tables and puts NULL in the columns where related records do not exist in the opposite table
SQL joins

MySQL queries on multiple tables

I know all about joining tables, but I'm having trouble figuring out what to do in this case.
I have a table users which contains a column called user_id which is an integer.
I have a second table called "follows" which contains a column "user_id" and a column called "follow_id". This table is used to log which other users each user follows (think Twitter).
I have a third table called posts which contains a column called user_id , which is a foreign key.
Ideally I'd like a single query that looks up the follows table, gets all the follow_ids from for a single user, then returns all posts from the posts table.
I suggest you to use LEFT JOIN
SELECT
follows.follow_id,
posts.*
FROM
follows
LEFT JOIN
post ON posts.user_id = follows.user_id
WHERE
follows.user_id = <UserID>

How do I combine data from two MySQL tables to return a list of users sorted by the count of user entries in one table?

I am currently using this MySQL query:
$query="SELECT user, COUNT(*) AS num FROM tracks GROUP BY user ORDER BY COUNT(*) DESC";
which gives me a unique row for each user with a count of the number of entries for each user in the table 'tracks'.
Not all users have an entry in the table 'tracks' though, and I would like to select/list all users on a page. All users are however included in another MySQL table called 'users'.
Because I would like to display all users sorted by the count of entries in the 'tracks' table (with users with no entries in the 'tracks' table at the bottom), I presume that I need to use one SELECT query to achieve this, but I am not sure how to do this, and would be glad of some help.
You need to do a left join. Assuming you have a "users" table with a "user_id" column which matches the "user" column in the "tracks" table:
SELECT users.user_id, count(tracks.user) from users left join tracks on (users.user_id = tracks.user) group by tracks.user order by count(tracks.user) DESC;

How do I select a record from one table in a mySQL database, based on the existence of data in a second?

Please forgive my ignorance here. SQL is decidedly one of the biggest "gaps" in my education that I'm working on correcting, come October. Here's the scenario:
I have two tables in a DB that I need to access certain data from. One is users, and the other is conversation_log. The basic structure is outlined below:
users:
id (INT)
name (TXT)
conversation_log
userid (INT) // same value as id in users - actually the only field in this table I want to check
input (TXT)
response (TXT)
(note that I'm only listing the structure for the fields that are {or could be} relevant to the current challenge)
What I want to do is return a list of names from the users table that have at least one record in the conversation_log table. Currently, I'm doing this with two separate SQL statements, with the one that checks for records in conversation_log being called hundreds, if not thousands of times, once for each userid, just to see if records exist for that id.
Currently, the two SQL statements are as follows:
select id from users where 1; (gets the list of userid values for the next query)
select id from conversation_log where userid = $userId limit 1; (checks for existing records)
Right now I have 4,000+ users listed in the users table. I'm sure that you can imagine just how long this method takes. I know there's an easier, more efficient way to do this, but being self-taught, this is something that I have yet to learn. Any help would be greatly appreciated.
You have to do what is called a 'Join'. This, um, joins the rows of two tables together based on values they have in common.
See if this makes sense to you:
SELECT DISTINCT users.name
FROM users JOIN conversation_log ON users.id = converation_log.userid
Now JOIN by itself is an "inner join", which means that it will only return rows that both tables have in common. In other words, if a specific conversation_log.userid doesn't exist, it won't return any part of the row, user or conversation log, for that userid.
Also, +1 for having a clearly worded question : )
EDIT: I added a "DISTINCT", which means to filter out all of the duplicates. If a user appeared in more than one conversation_log row, and you didn't have DISTINCT, you would get the user's name more than once. This is because JOIN does a cartesian product, or does every possible combination of rows from each table that match your JOIN ON criteria.
Something like this:
SELECT *
FROM users
WHERE EXISTS (
SELECT *
FROM conversation_log
WHERE users.id = conversation_log.userid
)
In plain English: select every row from users, such that there is at least one row from conversation_log with the matching userid.
What you need to read is JOIN syntax.
SELECT count(*), users.name
FROM users left join conversion_log on users.id = conversation_log.userid
Group by users.name
You could add at the end if you wanted
HAVING count(*) > 0