How to select data from one table and select count to another table and combine it in MySQL?

Pablo Picasso

In one fruit there can be multiple tickets that can be raised. I need to display the number of tickets raised per fruit. Their key field is the fruit_id.

sample output picture

Edgar Ramírez Mondragón

If I have the following tables:

fruit

id    name
 1    apple
 2    orange

tickets

id  fruit_id
 1         1
 2         1
 3         2
 4         2
 5         2

Then I would use the following SQL syntax to output a table like the one you need:

SELECT fruit.id, fruit.name, COUNT(tickets.id)
FROM tickets
LEFT JOIN fruit ON fruit.id = tickets.fruit_id
GROUP BY fruit.id;

Output:

id     name   COUNT(tickets.id)
 1    apple                  2
 2   orange                  3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MySQL query - select from one, count from another table

Select from one table and count from another

MYSQL select from table and count from another

How to select data from a table by referring to another table in MySQL

how to select a record from one table and count the number of which appears another table and count it

MySQL SELECT with data from another table

MySQL: How to select data from a column in another table

MYSQL: How do select column depending on data from another table?

Select multiple data from one column and update the combine result to another table

Select data from one table that depends on values from another table in mysql

select from one table, count from another where id is not linked

How to get selected value in one select option and based on that, fetch data from MySQL table to show another select option in the same form

How to select only rows from a table one clumns value is more than b table count value in MySQL

How to select rows from one data.table to apply in another data.table?

SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table

How to insert into one table null value and select * from another table in mysql?

How to select data(with one decimal type) from a table and insert into another table (with different decimal type)

How to select data from one table and insert it into another table with a new column

Select data from one table with the conditions from another

How to select from a table based on another table in mysql

SELECT from table with count from another

How to select data from a table and insert into another table?

How to select data from the table not exist in another table sql

How to select data from another table if it does not exist on the other table in mysql

How to select / join some data from one mySQL innodb table to another with no duplicates and choosing the last inserted row per id

mysql insert from one table to another based on select

How to select all data from one table and records from another table matching data in first selection. All in one query

best way to select data from one table or another

Select data from one table based on selected rows in another