get members count with family detail and hof detail mysql - mysql

persons table
-----------------
id name fatherid motherid famid
---------------------
1 ras lovely beautiful 5
2 abc def ghi 5
3 xyz abc aay 4
family table
----------------
famid hof ( id from persons)
------------------------
4 3
5 2
output required
-------------------
membercount(id with famid) hof_name hof_id(id from person) famid
-------------------
2 abc 2 5
1 xyz 3 4
please help me to get the correct query

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.

One to many relationship query result in a single row and dynamic multiple columns -SQL

Note : I cannot change Table structure
These are the three tables- user,user_field_type and user_field_value which needs to be joined and produce the result shown below
user
User_code User_id
-----------------
test11 000_1
test12 000_2
test13 000_3
test14 000_5
user_field_type
field_name field_id
----------------------
Name 100_1
Age 100_2
Class 100_3
Roll 100_4
User_field_value
user_id field_id field_value
-------------------------------------
000_1 100_1 Tom
000_1 100_2 6
000_1 100_3 2
000_1 100_4 1
000_2 100_1 Dick
000_2 100_2 6
000_2 100_3 2
000_2 100_4 2
000_3 100_1 Harry
000_3 100_2 6
000_3 100_3 2
000_3 100_4 3
Result Needed:
user_id user_code Name Age Class Roll
------------------------------------------------
000_1 test11 Tom 6 2 1
000_2 test12 Dick 6 2 2
000_3 test13 Harry 6 2 3
I tried Group concat which doesn't produce desired results and i'm afraid of hard coding columns as it will require changes everytime we add a new user field type

How to perform these joins with MySQL?

Having these tables:
clients
-------------------
id
name
town
companies
------------------
id
name
credits
------------------
clients_id
companies_id
credit
Example:
clients
-------------
1 john doe London
2 jane smith Paris
companies
-------------
1 mycompany1
2 mycompany2
credits
-------------
1 1 5000
1 2 3250
2 2 4500
How can I list all the clients including an additional column for each credit she has in a company? I also need to alias each of these columns in this fashion: "credit_"
Something like:
id name town credit_1 credit_2
1 john doe London 5000 3250
2 jane smith Paris NULL 4500
I'm not sure if doing two LEFT JOINS to credits would work.

In SQL select and count all entries where with same value in column

I have a table like in an mysql-database.
id section number name
1 A 1234 fred
2 B 5678 mo
3 B 1234 fred
4 C 8901 lou
5 A 8901 lou
6 A 2345 lee
7 B 2345 lee
8 C 2345 lee
9 A 6789 paul
10 B 1234 fred
This is my table and i need to get all numbers is in more then one section with the name of the section. like this
id section number name
1 A 1234 fred
3 B 1234 fred
Fred is in section A and B.
In my solution i get this
id section number count name
1 A 1234 1 fred
3 B 1234 2 fred
6 A 2345 1 lee
7 B 2345 1 lee
8 C 2345 1 lee
2 B 5678 1 mo
9 A 6789 1 paul
4 C 8901 1 lou
5 A 8901 1 lou
in my way i try is sql select
SELECT id, section, number, count(number) AS count, name
FROM table
GROUP BY section, number
ORDER BY number
Is is the right way? How can i improve that for mor number columns, if have 3 numbercolumns
id section number1 number2 number3 name
Now i do this with 3 sql-statements

How to write sql query for this

**CategoryMaster**
CategoryId Category Name
1 ABC
2 Xyz
3 PQR
**Product master**
ProductId Name
1 ABCD
2 WXYZ
3 UVWX
**ProductCategory**
ProCatID ProductId CategoryId
1 1 1
2 1 2
3 2 3
4 3 2
5 3 3
**ProductDetail**
ProductDetailId Price Qty Date ProductId
1 250 8 2-11-2011 1
2 200 10 2-12-2011 2
I want to featch all product detail from product master table with its category my table structure is same as above
Your ProductMaster table is not normalized. This is not good: you should model like this:
Productmaster
- productid
- name
ProductCategories:
- productid
- categoryID