Returning a single record from many to many table

perkes456

I have a table structure like following:

Users => Histories <= Brands

Histories table keeps following columns: userId, brandId, points

I have a query like following:

select b.id, sum(h.points),h.brandId
from brands b left join
     histories h
     on b.id = h.brandid and h.userId = 2866
group by b.id;

This query returns me brands where USER made points and didnt make any points at all..

I wanna add a filter to a brandId as well so that the query doesn't returns all results, but a single result at a time depending what brandId has been sent to the query... How can I and where can I add a statement which would return me a single record for this query?? So basically I just need to pass brandId = to something statement, leaving the query it self as it is right now ... :)

Rene Korss

For example if brand id is 100:

select b.id, sum(h.points),h.brandId
from brands b left join
     histories h
     on b.id = h.brandid and h.userId = 2866
where b.id=100
group by b.id
limit 1;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

LINQ to SQL select exact matching record from many to many table

From Dev

Create many to many from single table performance issue

From Dev

Many-to-Many relationship in Access from a single table

From Dev

Returning an array from a method and storing it in a single variable and many ones

From Dev

Get record in many to many table with null row

From Dev

Get record in many to many table with null row

From Dev

Postgres select from one to many table to single table rows

From Dev

Returning nested JSON from many to many joined table from PostgreSQL to node.js

From Dev

Returning nested JSON from many to many joined table from PostgreSQL to node.js

From Dev

Return latest record from 1 to Many Relationship table

From Dev

Select many intervals from a single table based on column value

From Dev

Getting all the info from many normalized tables that are related to a single table

From Dev

sql query in many to many relation returns single record

From Dev

MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

From Dev

Selecting from the table that does many to many

From Dev

Lambda get from many to many table

From Dev

How to concatenate data from a many to many table?

From Dev

Select from many-to-many table

From Dev

Empty data returning from many to many relationship laravel 5

From Dev

SQL for returning values from a many-to-many relationship

From Dev

SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

From Dev

Constraint of a single True value in many-to-many through table BooleanField

From Dev

Many-to-Many Relationship Against a Single, Large Table

From Dev

How express the many to many relationship with single parent table in EF 5

From Dev

SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

From Dev

How to select multiple many to many in relation with a single table

From Dev

Doctrine single table inheritance - many-to-many assocication not working

From Dev

In a single SQL query how many indexes can the query use from a single table?

From Dev

In a single SQL query how many indexes can the query use from a single table?

Related Related

  1. 1

    LINQ to SQL select exact matching record from many to many table

  2. 2

    Create many to many from single table performance issue

  3. 3

    Many-to-Many relationship in Access from a single table

  4. 4

    Returning an array from a method and storing it in a single variable and many ones

  5. 5

    Get record in many to many table with null row

  6. 6

    Get record in many to many table with null row

  7. 7

    Postgres select from one to many table to single table rows

  8. 8

    Returning nested JSON from many to many joined table from PostgreSQL to node.js

  9. 9

    Returning nested JSON from many to many joined table from PostgreSQL to node.js

  10. 10

    Return latest record from 1 to Many Relationship table

  11. 11

    Select many intervals from a single table based on column value

  12. 12

    Getting all the info from many normalized tables that are related to a single table

  13. 13

    sql query in many to many relation returns single record

  14. 14

    MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

  15. 15

    Selecting from the table that does many to many

  16. 16

    Lambda get from many to many table

  17. 17

    How to concatenate data from a many to many table?

  18. 18

    Select from many-to-many table

  19. 19

    Empty data returning from many to many relationship laravel 5

  20. 20

    SQL for returning values from a many-to-many relationship

  21. 21

    SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

  22. 22

    Constraint of a single True value in many-to-many through table BooleanField

  23. 23

    Many-to-Many Relationship Against a Single, Large Table

  24. 24

    How express the many to many relationship with single parent table in EF 5

  25. 25

    SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError

  26. 26

    How to select multiple many to many in relation with a single table

  27. 27

    Doctrine single table inheritance - many-to-many assocication not working

  28. 28

    In a single SQL query how many indexes can the query use from a single table?

  29. 29

    In a single SQL query how many indexes can the query use from a single table?

HotTag

Archive