PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 12, 2022

[FIXED] Need a hand with joins SQL

 January 12, 2022     mysql, phpmyadmin, sql     No comments   

Issue

Hi guys so what I think I have to do is a double join, but I have done some research and still I am not quite sure how to do it ... hope you guys can give me a hand. So below are my tables.

enter image description here

enter image description here

enter image description here

As you probably see, the empresa is related to empresa_id on the second table, and id_user from the third table is related to id of the second table.

I've already done this query :

select users.username, COUNT(*) c
from form_reply
    left join users on form_reply.id_user = users.id
GROUP BY id

but it only retrieves the number of rows of each user and I also want it's "empresa". I am expecting an output like this :

+--------+-+----------+
|Username|c| empresa  |
+--------+-+----------+
|   su   |7|AccSystems|
+--------+-+----------+

enter image description here


Solution

use another join with empressa_table to combine 3 tables

select users.username,e.empresa, COUNT(*) c
from form_reply
    join users on form_reply.id_user = users.id
    join empressa_table e on users.empresa_id=e.id
GROUP BY users.username,e.empresa


Answered By - Fahmi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing