0

There are 2 tables: subjects and messages.

subjects table:

----id----   ----title----
1            Subject 1
2            Subject 2
3            Subject 3

messages table:

----id----   ----subject----   ----message----
1            1                 Message to Subject 1
2            1                 Message to Subject 1
3            2                 Message to Subject 2

How to get mysql_num_rows of subjects, which has got any messages in messages? The result must be 2, because Subject 1 has message in messages, and Subject 2 has message in messages, but Subject 3 hasn't got any message in messages.

Something like:

mysql_num_rows(mysql_query("SELECT * FROM subjects ...

4
  • 2
    Where is your query ? What you have tried so far? Commented May 22, 2017 at 12:28
  • 2
    select count( distinct subject ) from messages I think Commented May 22, 2017 at 12:28
  • @OtoShavadze can we select directly from subjects? Commented May 22, 2017 at 12:30
  • 2
    STOP using deprecated mysql_* API. Use mysqli_* or PDO Commented May 22, 2017 at 12:30

4 Answers 4

4

You would normally use in or exists for this purpose:

select count(*)
from subjects s
where exists (select 1 from messages m where m.subject_id = s.id);

This can easily be modified to get the subjects with no messages.

If you have a proper foreign key relationship defined between the tables, then you can just count the subjects in messages:

select count(distinct m.subject_id)
from messages;

Aggregation in MySQL is quite expensive. There are circumstances where this will perform better. However, the existsis likely to perform better under most circumstances, assuming you have an index onmessages(subject_id)`.

Sign up to request clarification or add additional context in comments.

4 Comments

The select count( distinct subject ) from messages from Oto seem a better idea than the exists option for me...
why will select count( distinct subject ) from messages not work...out of curiosity and learning.
Oh, thank you, for your answer, it's the right answer, I needed selection directly from subjects!
@PrabhatG because I need to select queries from subjects, not from messages.
0

TRY THIS: I think we can simply do it using INNER JOIN

SELECT COUNT(mes.subject) total_msg
FROM subjects sub
INNER JOIN messages mes on mes.subject_id = sub.id

Comments

0

Try this

$result = $mysqli->query("SELECT messages.id, messages.message, 
        subjects.title,subjects.id as subjectID
        FROM messages
        INNER JOIN subjects
        ON messages.subject=subjects.id");
    $row_cnt = $result->num_rows;
    echo $row_cnt;

Comments

0

If you want to search for title in the messages, then try this.

Rextester Demo

select count(s.title) as result 
from subjects s
where exists (select 1 from messages m
                where instr(m.message,s.title)> 1
             )

Explanation: instr(m.message,s.title) will search for title in messages and return the start position (integer). If not found, you will get 0. So instr(m.message,s.title)> 1 will only be true for the expected matches.

Then count(s.title) will give you distinct count.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.