Mysql Inner Joın At least one match is required [duplicate] - mysql

This question already has answers here:
How to select rows with no matching entry in another table?
(11 answers)
How to return rows from left table not found in right table?
(7 answers)
Selecting all items in one table and join with another table, allowing nulls
(4 answers)
Closed 3 years ago.
I created two tables. I keep the topics in one and the comments in this topic in the other.
When I want to view the topics, I want to see the total number of comments on the topic from the comments table.
But if there is not at least one comment in the query I wrote, it does not bring results. How can I see my topics even if there is no comment on my Inner Join query?
Thank you.
Here is my Query:
SELECT portal_topics.id, portal_topics.topicDetail, portal_topics.topicName,portal_topics.topicCategory,portal_topics.topicAuthor, portal_topics.topicTıme, portal_topics.topicAvatar,portal_topics.topicViews, portal_topics.topicLikes, COUNT(portal_comments.id) as totalCommentCount FROM `portal_topics`
INNER JOIN portal_comments ON portal_comments.topic_id = portal_topics.id
WHERE portal_comments.topic_id = portal_topics.id
GROUP BY portal_topics.topicName
Note: The query does not contain any Syntax errors. I have arranged the column names in English for everyone to understand. I may have made a mistake in this section.

Related

How to get the items not found in a Mysql table [duplicate]

This question already has answers here:
SQL IN Clause - get back IN elements that did not match
(5 answers)
Closed 7 months ago.
I have a random input of identifiers (primary key), and I need to return the identifiers which were not found in the mySql table.
Lets say my identifier list is ['FEB221571','1221SC170','75960620447200111', 'ABC12344'].
My table has the first three identifiers so I can find them on the table but I wish the last one to be returned as it cannot be found in the table. How do I implement that?
select * from identifier_list a
where not exists (select 'x' from some_table b and a.id = b.id);

Reverse effect of Join in mysql [duplicate]

This question already has answers here:
MYSQL Left Join how do I select NULL values?
(4 answers)
Closed 4 years ago.
I'm developing a system and creating invoices. I want the record where no invoice has been created.
I'm trying to write a MySQL query, where I need the records whch can not be joined with another table. In other words, the records that do not have a linked record in the other table.
I tried the following query
SELECT exports.id as e_id,export_invoices.id as i_id
FROM exports
LEFT JOIN export_invoices ON export_invoices.export_id = exports.id
and got this result:
Which gives all value and also the record of which invoice is not created with NULL value (I want to have that [e_id->2 from result]). I just want to extract that null value record's master id.
Simply add the where condition in your query -
SELECT exports.id as e_id,export_invoices.id as i_id
FROM exports LEFT JOIN export_invoices on export_invoices.export_id = exports.id
WHERE export_invoices.id IS NULL;

Is there any difference between inner join and table separated with a comma? [duplicate]

This question already has answers here:
What's the difference between comma separated joins and join on syntax in MySQL? [duplicate]
(5 answers)
Closed 6 years ago.
Is there any performance difference between these two query. Both query returns the same result.
select * from tbl_a, tbl_b
and
select * from tbl_a INNER JOIN tbl_b
There is a thread already touching on this topic. Check it out:
What is the difference between inner join and outer join
Performance wise, it will all come down to the amount of data you're working with. A couple tables with a few hundred rows won't make a dent, but you will notice a difference as the amount of data sorted grows.
Good luck!

find column who has no corresponding rows in another table [duplicate]

This question already has an answer here:
Select rows from one table, join most recent row from other table with one-to-many relationship
(1 answer)
Closed 7 years ago.
Friends i have a problem
I have two tables channel and level
channel table
channel_id, manufacturer_id
level table
level_id,channel_id,sort_order
All channels have certain number of levels in them so like if channel has 4 rows level might have 20.
I want to know which channel has no levels at all. I have tried various solutions from stackoverflow.com but none worked for me as all of them return an empty set but I still have channels with no levels in the database. Please can anybody help. Thanks
You need to do anti join something as
select
c.* from channel c
left join level l on l.channel_id = c.channel_id
where l.channel_id is null

Finding records that don't share a relationship [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL - find records from one table which don’t exist in another
I have the following (simplified) schema in MySQL:
An arrow indicates a one (non-arrow side) to many (arrow side) relationship.
I want to determine, for which delivery_zone_weeks, does a customer not have a weekly_order.
It is difficult to understand fully without structures, sample data and expected result, but seems to be risking it a bit you need
SELECT * FROM DELIVERY_ZONE_WEEK WHERE ID_DELIVERY_ZONE_WEEK NOT IN
(SELECT WO.ID_DELIVERY_ZONE_WEEK FROM CUSTOMER C
JOIN SHIPPING_ADDRESS SA
ON C.ID_CUSTOMER = SA.ID_CUSTOMER
JOIN WEEKLY_ORDER WO
ON SA.ID_SHIPPING = WO.ID_SHIPPING
WHERE C.ID_CUSTOMER = #ID_CUSTOMER)