Make a SELECT query from two tables with one record from matching rows in mysql

user3733831

I do have two mysql tables named Services and Images. Each service may have multiple images.

So my question is, just I need to make a SELECT query to get all services with one image to each service.

This is how I tried it. But it gave me all services and all images to each service.

SELECT s.id
        , s.name
        , s.description
        , i.id as image_id
        , i.image
FROM  services s
LEFT JOIN images i ON i.service_id = s.id;

Can anybody tell me how I make this query correctly?

Thank you.

Imran

IF you need just one image for one service you can group by your service id

SELECT s.id
        , s.name
        , s.description
        , i.id as image_id
        , i.image
FROM  services s
LEFT JOIN images i ON i.service_id = s.id
GROUP BY s.id;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL - one to many select from two tables

From Dev

How to select all matching rows from three tables by join query

From Dev

query to select non matching records from two tables

From Dev

MySQL How can I select data from two tables so that rows from the second one overwrote those from the first one?

From Dev

Select random rows from multiple tables in one query

From Dev

How to count rows from two tables in one query?

From Dev

How to count rows from two tables in one query?

From Dev

How to select non-matching rows from two mysql table

From Dev

MySQL select row with two matching joined rows from another table

From Dev

MySQL query to select one table based on values from two different tables.

From Dev

Selecting one record from two tables and multiple records from another table in ONE query

From Dev

Query syntax with php variables to select from two MySQL tables

From Dev

How can I select from different tables in one MySQL query?

From Dev

select matching data from mysql tables

From Dev

select matching data from mysql tables

From Dev

How select rows by column from two tables

From Dev

How select rows by column from two tables

From Dev

SQL query for returning rows from two tables

From Dev

Delete rows from multiple tables as one query

From Dev

Mysql Select with two SUM from two tables

From Dev

select two logins from one record

From Dev

MYSQL Select from tables based on multiple rows

From Dev

PHP and MySql - fetching rows from two tables

From Dev

query to select from two tables on symfony

From Dev

Select Query from two tables in codeigniter

From Dev

Select rows from a table based on results from two other tables in mySQL using PDO

From Dev

MySQL query - selecting from two record sets

From Dev

MySQL project design - conditionally select from one table based on rows from another select query

From Dev

Matching data from two tables with unequal number of rows

Related Related

  1. 1

    MySQL - one to many select from two tables

  2. 2

    How to select all matching rows from three tables by join query

  3. 3

    query to select non matching records from two tables

  4. 4

    MySQL How can I select data from two tables so that rows from the second one overwrote those from the first one?

  5. 5

    Select random rows from multiple tables in one query

  6. 6

    How to count rows from two tables in one query?

  7. 7

    How to count rows from two tables in one query?

  8. 8

    How to select non-matching rows from two mysql table

  9. 9

    MySQL select row with two matching joined rows from another table

  10. 10

    MySQL query to select one table based on values from two different tables.

  11. 11

    Selecting one record from two tables and multiple records from another table in ONE query

  12. 12

    Query syntax with php variables to select from two MySQL tables

  13. 13

    How can I select from different tables in one MySQL query?

  14. 14

    select matching data from mysql tables

  15. 15

    select matching data from mysql tables

  16. 16

    How select rows by column from two tables

  17. 17

    How select rows by column from two tables

  18. 18

    SQL query for returning rows from two tables

  19. 19

    Delete rows from multiple tables as one query

  20. 20

    Mysql Select with two SUM from two tables

  21. 21

    select two logins from one record

  22. 22

    MYSQL Select from tables based on multiple rows

  23. 23

    PHP and MySql - fetching rows from two tables

  24. 24

    query to select from two tables on symfony

  25. 25

    Select Query from two tables in codeigniter

  26. 26

    Select rows from a table based on results from two other tables in mySQL using PDO

  27. 27

    MySQL query - selecting from two record sets

  28. 28

    MySQL project design - conditionally select from one table based on rows from another select query

  29. 29

    Matching data from two tables with unequal number of rows

HotTag

Archive