Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get results by count and operators in one query

I think question is wrong, but i don't know how to ask properly.

This query selects all businesses with all workers, whose job_types contains C letter.

$connect = DB::table('relationship')
->join('workers', 'workers.id', '=', 'relationship.w_id')
->join('business', 'business.id', '=', 'relationship.b_id')
->whereRaw('job_types LIKE "%C%"')
->groupBy('relationship.w_id')
->get();

I am using foreach for displaying results

foreach ($connect as $item) {
 echo $item->name;
 // etc
}

I want to select all businesses who have more than 3 or less than 3 or equal to 3 (depends of what i need) job_types LIKE "%C%" and store information like that:

1. APPLE | Tom | C
2. APPLE | Tim | C
3. APPLE | Jeff | C
4. IBM | Jenny | C
5. IBM | Sean | C
6. IBM | Ian | C
// etc``

Answer by @KikiTheOne is kinda working, but it does not display results as needed.

like image 456
Tauras Avatar asked Oct 12 '16 12:10

Tauras


People also ask

Can we use GROUP BY and COUNT together in SQL?

The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

How do I use COUNT and distinct together in SQL?

The correct syntax for using COUNT(DISTINCT) is: SELECT COUNT(DISTINCT Column1) FROM Table; The distinct count will be based off the column in parenthesis. The result set should only be one row, an integer/number of the column you're counting distinct values of.

Can we use COUNT and GROUP BY together?

The use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same values (on a column) will be treated as an individual group.

Can sum and COUNT in same SQL query?

SUM() and COUNT() functions SUM of values of a field or column of a SQL table, generated using SQL SUM() function can be stored in a variable or temporary column referred as alias. The same approach can be used with SQL COUNT() function too.


1 Answers

*****SOLUTION*****

SELECT 
 * 
FROM 
    people_details as t1 
inner join 
    people_branches as t2 
on t1.id = t2.id 
    inner join 
    ( 
        SELECT 
            count(t1.id) as worker_counter,t1.branch_id
        FROM 
            people_branches as t1 
                inner join people_details as t2 
                    on t1.id = t2.id 
        WHERE 
            t2.job_types LIKE '%C%' 
            group by branch_id 
    ) as t3 
    on t2.branch_id = t3.branch_id 
inner join people_frontpage as t4 
    on t4.id = t1.id 
inner join business as t5 
    on t5.id = t2.branch_id 
WHERE 
    t1.job_types LIKE '%C%' 
    AND t3.worker_counter > 200

----------


OLD - UPDATE

  1. Table Business
  2. Table Relationship
  3. Table Worker
  4. Output

SELECT 
    t3.bus_name, t1.name, t1.job_types 
FROM 
    SO_WORKER as t1 
    inner join 
        SO_RELATIONSHIP as t2 
            on t1.id = t2.w_id 
    inner join 
    (
        SELECT 
            count(t1.w_id) as worker_counter,t1.b_id,t3.bus_name 
        FROM 
            SO_RELATIONSHIP as t1 
            inner join SO_WORKER as t2 
                on t1.w_id = t2.id 
            inner join SO_BUSINESS as t3 
                on t3.id = t1.b_id 
        WHERE 
            t2.job_types LIKE '%C%' 
            group by b_id
    ) as t3 
    on t2.b_id = t3.b_id 
WHERE t1.job_types LIKE '%C%'
AND t3.worker_counter <= 3

Unformated

SELECT t3.bus_name, t1.name, t1.job_types FROM SO_WORKER as t1 inner join SO_RELATIONSHIP as t2 on t1.id = t2.w_id inner join (SELECT count(t1.w_id) as worker_counter,t1.b_id,t3.bus_name FROM SO_RELATIONSHIP as t1 inner join SO_WORKER as t2 on t1.w_id = t2.id inner join SO_BUSINESS as t3 on t3.id = t1.b_id WHERE t2.job_types LIKE '%C%' group by b_id) as t3 on t2.b_id = t3.b_id WHERE t1.job_types LIKE '%C%' AND t3.worker_counter <= 3

--------------------------------------------------

OLD CODE

in Relation to the comments from Post 1.

Table: SO_BUSINESS
    id      |       bus_name
    --------------------
    1       |       BUSI A
    2       |       BUSI B

Table: SO_WORKER
    id      |       job_types
    ---------------------
    1       |       CEO
    2       |       GFO
    3       |       CTO
    4       |       Manager
    5       |       Worker

Table: SO_RELATIONSHIP
    w_id    |       b_id
    ----------------
    1       |       1
    2       |       2
    3       |       1
    4       |       1
    5       |       2

Query: Output
    workers_count   |       b_id        |       bus_name
    --------------------------------------------
    2               |       1           |       BUSI A

.

SELECT * 
FROM 
    (
        SELECT 
            count(t1.w_id) as workers_count,
            t1.b_id,
            t3.bus_name
        FROM 
            SO_RELATIONSHIP as t1 
            inner join 
                SO_WORKER as t2 on t1.w_id = t2.id 
            inner join 
                SO_BUSINESS as t3 on t1.b_id = t3.id 
        WHERE 
            t2.job_types LIKE '%C%' 
        GROUP BY t1.b_id
    ) as t4 
WHERE 
    t4.workers_count < 3  

Code unformated:

SELECT * FROM (SELECT count(t1.w_id) as workers_count,t1.b_id,t3.bus_name FROM SO_RELATIONSHIP as t1 inner join SO_WORKER as t2 on t1.w_id = t2.id inner join SO_BUSINESS as t3 on t1.b_id = t3.id WHERE t2.job_types LIKE '%C%' GROUP BY t1.b_id) as t4 WHERE t4.workers_count < 3  

Let me know if this helps u

like image 163
KikiTheOne Avatar answered Oct 10 '22 07:10

KikiTheOne