MySQL queries on multiple tables - mysql

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>

Related

In vTiger 6.5: Which table stores the products that belongs to a quotes?

I need to know which table acts as an intermediary to achieve the many-to-many relationship between these entities.
I know that the table that stores the products is vtiger_products and that the one that keeps the quotes is vtiger_quotes but I do not know which table relates both, so my query is incomplete.
So...
SELECT * FROM vtiger_quotes
INNER JOIN vtiger_products INNER JOIN table_relates_both
ON vtiger_quotes.quoteid = table_relates_both.quoteid
AND vtiger_products.productid = table_relates_both.productid
WHERE vtiger_quotes.potentialid = ?
What's the real name of table_relates_both?
vtiger_inventoryproductrel is the intermediary table between vtiger_quotes and vtiger_products
Below is the structure of vtiger_inventoryproductrel where id column act as a foreign key of Quotes, Opportunity, Invoice etc
If you want to fetch Quotes related to particular Opportunity then you need to execute below query:
SELECT {your required field goes here} FROM vtiger_inventoryproductrel INNER JOIN vtiger_quotes
ON vtiger_quotes.quoteid = vtiger_inventoryproductrel.id
WHERE vtiger_quotes.potentialid = $potential_id
Also note that:
vtiger_crmentity - This is core table in which an entry is added for
all entity type records. This stores meta information like record id,
record owner id, last modified by user id, created time, modified time
and description.
the table name is vtiger_inventoryproductrel

SQL Join getting like count of a post

I have a table that contains the information of all the posts, table's name is "paylasimlar". and I have another table that contains the information of every like action, table's name is "begeniler". It has 2 columns:
1-User ID of who liked the post
2-The Post ID that liked
The thing I want to do is to write a query with joins that returns all the information of the posts from table "paylasimlar" with the count of likes it got. The problem I ran into is; if a post hasn't got liked yet there would be no information on the "begeniler" table and it would not return the information of that table as a row. Can someone help?
select a.*,count(b.PostID) from paylasimlar a
left join begeniler b on a.PostID and b.PostID group by b.PostID

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

mysql compare two tables

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