SQLite COUNT and selecting from multiple tables - mysql

Consider the database (made up for example):
Table 'owner'
id name
1 john
2 andrew
3 peter
Table 'duck'
id name ownerID
1 duck1 1
2 duck2 1
3 duck3 1
4 duck4 2
5 duck5 2
6 duck6 3
7 duck7 3
8 duck8 1
9 duck9 3
10 duck10 3
11 duck11 1
12 duck12 2
Table 'food'
id name type duckID
1 beef meat 4
2 grass veg 8
3 lemon fruit 5
4 apple fruit 3
5 pizza snack 7
I wish to write some SQL that for each OWNER, COUNT the number of ducks which eat some kind of food. For example for owner John, there would be 2 ducks (ducks 3 and 8).
So far I am writing this code:
select owner.name, count(duck.ownerID)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id;
I am getting the result:
Peter | 5
Any suggestions are appreciated.

This is done with an group by clause:
select owner.name, food.name, count(duck.id)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id
group by owner.name, food.name;
This query gives you one row for each combination of owner name and food name with the number of the owner's ducks eating this food.

Related

Gaming Database Query for multiple players

I want to have a SQL result look like this (match result query):
ID Arena Winner Loser Winner_score Loser_score
-----------------------------------------------------------------
1 1 Johnny Mark 16 8
2 2 Andrew Luke 16 7
Here are my tables (simplified)
Player_tbl
ID Player
-------------
1 Johnny
2 Mark
3 Andrew
4 Luke
Match_tbl
ID Match Arena
----------------------
1 Match 1 1
2 Match 2 2
Match_results_tbl
ID Match player_id player_score
-----------------------------------------
1 Match 1 1 16
2 Match 1 2 8
1 Match 2 3 16
2 Match 2 4 7
I am wondering how to structure my query to have the desired result.
Ok, I figured out my own issue. 1st, the match_results must be put in a table with a different structure, and 2nd, the inner sql query needed to be adjusted to pull from the proper database tables.

how to display result with order by based parent and childs in one table

I got problem with how to display query result based by parent and child on one table
data for example:
ID
CATEGORY
PARENT
ROOT
1
FOOD & DRINK
0
NULL
2
CLOTHES
0
NULL
3
FRUIT
1
1
4
DESERT
1
1
5
PANTS
2
2
6
T-SHIRT
2
2
7
APPLE
3
1
8
BANANA
3
1
9
ICE CREAM
4
1
10
SHORTS
5
2
11
LONG T-SHIRT
6
2
and I want to sort and display to this:
ID
CATEGORY
PARENT
ROOT
2
CLOTHES
0
NULL
5
PANTS
2
2
10
SHORTS
5
2
6
T-SHIRT
2
2
11
LONG T-SHIRT
6
2
1
FOOD & DRINK
0
NULL
4
DESERT
1
1
9
ICE CREAM
4
1
3
FRUIT
1
1
7
APPLE
3
1
8
BANANA
3
1
is there any query script to display like above ?
here my code and have no luck
select
id, category, parent, root
from
tbl_menu
order by
coalesce(root,parent,id),
category
and thanks for help :)
I understand that you want to order the tree nodes depth-first. One option uses a recursive query to generate the path to each node, that you can then use for ordering:
with recursive cte as (
select t.*, cast(id as char) as path from mytable t where root is null
union all
select t.*, concat(c.path, '/', t.id)
from cte c
inner join mytable t on t.parent = c.id
)
select *
from cte
order by path

Putting a table result as a text column

I have two tables, the first one is a table of companies, and the second one is a table of shareholders we could say. The shareholder table can have upwards of a thousand shareholders.
I need to make a view/another table which has every shareholder name on a single text column along with the unique identifier of the company, the name and "fake" name of the company.
Table 1 Company:
ID CompanyID Name FantasyName
1 1 A Company 1
2 2 B Company 2
3 3 C Company 3
4 4 D Company 4
5 5 E Company 5
6 6 F Company 6
7 7 G Company 7
Table 2 Shareholders:
ID CompanyID Name
1 1 John 1
2 1 Peter 2
3 1 Gabriel Li
4 2 Raphael 3
5 2 Anderson 4
6 2 Michael 6
7 2 Angelina Jo
8 3 John 8
9 4 Beatrice
10 4 Scarlet
11 5 Scarlet
12 5 Logan
13 5 John 1
I tried to do this via Linq through C# but it hasn't been fast enough for my purpose, this table contains upwards of millions of companies.
The end result would look like this:
Table 3 Company and Shareholders:
ID CompanyID Name FantasyName Shareholders
1 1 A Company 1 John 1,Peter 2,Gabriel Li
2 2 B Company 2 Raphael 3, Anderson 4,Michael 6,Angelina Jo
3 3 C Company 3 John 8
4 4 D Company 4 Beatrice,Scarlet
5 5 E Company 5 Scarlet,Logan,John 1
...
All shareholders in a single TEXT field, with the company info accompanying it.
You can use aggregation and GROUP_CONCAT():
CREATE VIEW myview AS
SELECT
c.ID,
c.CompanyID,
c.Name,
c.FantasyName,
GROUP_CONCAT(s.Name) Shareholders
FROM
Company c
INNER JOIN Shareholders s ON s.CompanyID = c.CompanyID
GROUP BY
c.ID,
c.CompanyID,
c.Name,
c.FantasyName

How to get total of parents categories?

I have two tables, the first is the following
"Categories"
id name parent_id
1 Food NULL
2 Vegetable 1
3 Fruit 1
4 Tomatoes 2
5 onion 2
6 bananas 3
7 grapes 3
the second table is the following
"amounts"
id amount category_id
1 100 4
2 50 6
3 25 5
4 100 7
5 50 4
6 25 7
Currently categories table are three levels and I want to return all
the Categories along with total columns:
Means that sum of the category of level 2 is the total of level 3 and
the total of level 2 should was the sum of level 1. I would really
appreciate some help Beforehand thank you very much.

DB Design: which relationship (One-To-Many or Many-To-Many) is best for this scenario?

Let me simplify this question.
Lets say that we have students and subjects.
Each Students can choose upto any 4 subjects.
Type A Design:
Students table
id Students_name
1 John
2 Jack
3 Jill
4 Nancy
Subjects table
id Subjects Students_id
1 Language 1
2 Maths 1
3 Science 1
4 History 1
5 Computer 2
6 Language 2
7 Maths 2
8 Science 2
9 History 3
10 Computer 3
11 Maths 3
12 Science 3
Type B Design
Same students table as type A
Subjects table
id Subjects
1 Language
2 Maths
3 Science
4 History
5 Computer
Another junction table is added here in this type as follows
students_subjects_mapping table
id Subjects_id Students_id
1 1 1
2 2 1
3 3 1
4 4 1
5 5 2
6 2 2
7 3 2
8 4 2
So which type is better for this scenario?