mysql LEF JOIN with LIMIT - php

I have 2 tables:
games:
g_id | country | team_1 | team_2
--------------------------------
1 | England | Bayern | Chelsea
2 | England | Bayern | Liverp
3 | England | Bayern | Ajax
statistic:
s_id | s_time | s_name | g_id
-----------------------------
1 | 4 | Alen A. | 1
2 | 7 | Dagn S. | 1
3 | 11 | Eden D. | 1
4 | 22 | Aren A. | 1
5 | 8 | Falen B. | 2
6 | 66 | Poker G. | 2
7 | 76 | Nuker S. | 2
8 | 87 | Eben Y. | 2
9 | 18 | Falen B. | 3
10 | 19 | Aalen F. | 3
11 | 33 | Gased G. | 3
12 | 44 | Halen B. | 3
And i'm trying to get data from 2 tables with left join where limit
here a query:
SELECT *
FROM games
LEFT JOIN statistic
ON games.g_id = statistic.g_id
WHERE games.team1 = 'Bayern'
LIMIT 2
result is:
g_id | country | team_1 | team_2 | s_id | s_time | s_name | g_id
------------------------------------------------------------------
1 | England | Bayern | Chelsea| 1 | 4 | Alen A. | 1
1 | England | Bayern | Chelsea| 2 | 7 | Dags S. | 1
i need all data from statistics with limit 2 from table "games"! here example what i need:
g_id | country | team_1 | team_2 | s_id | s_time | s_name | g_id
------------------------------------------------------------------
1 | England | Bayern | Chelsea| 1 | 4 | Alen A. | 1
1 | England | Bayern | Chelsea| 2 | 7 | Dags S. | 1
1 | England | Bayern | Chelsea| 3 | 11 | Eden D. | 1
1 | England | Bayern | Chelsea| 4 | 22 | Aren A. | 1
2 | England | Bayern | Liverp | 5 | 8 | Falen B. | 2
2 | England | Bayern | Liverp | 6 | 66 | Dags S. | 2
2 | England | Bayern | Liverp | 7 | 76 | Alen A. | 2
2 | England | Bayern | Liverp | 8 | 87 | Dags S. | 2
What query i need?

You can try this:
SELECT *
FROM (SELECT * FROM games WHERE team1 = 'Bayern' ORDER BY g_id LIMIT 2) AS g
LEFT JOIN statistic AS s
ON g.g_id = s.g_id

Related

Sales SUM per country, state, city and district from five Mysql tables

I have the five below tables, I want to show the sales SUM per country, state, city and district by using one mysql query if possible:
note: If there is no sales in the state, city, district, then I need the query result to show 0 or empty space (the state, city, district name has to be shown even if there is no sales for that specific state, city or district)
+------------+----------+
| country |
+------------+----------+
| country_id | country |
+------------+----------+
| 1 | country1 |
| 2 | country2 |
+------------+----------+
+----------------+--------+------------+
| state_province |
+----------------+--------+------------+
| state_id | state | country_id |
+----------------+--------+------------+
| 1 | state1 | 1 |
| 2 | state1 | 2 |
| 3 | state2 | 2 |
| 4 | state2 | 1 |
+----------------+--------+------------+
+---------+-------+----------+
| city |
+---------+-------+----------+
| city_id | city | state_id |
+---------+-------+----------+
| 1 | city1 | 1 |
| 2 | city2 | 1 |
| 3 | city1 | 3 |
| 4 | city2 | 3 |
| 5 | city1 | 4 |
| 6 | city2 | 4 |
+---------+-------+----------+
please note that state (state_id =2) in country (country_id = 2) has no cities in the above table.
+-------------+-----------+---------+
| district |
+-------------+-----------+---------+
| district_id | district | city_id |
+-------------+-----------+---------+
| 1 | district1 | 1 |
| 2 | district2 | 1 |
| 3 | district1 | 2 |
| 4 | district2 | 2 |
| 5 | district1 | 4 |
| 6 | district2 | 4 |
| 7 | district1 | 5 |
| 8 | district1 | 6 |
+-------------+-----------+---------+
+----------+------------+----------+---------+-------------+--------+
| sales |
+----------+------------+----------+---------+-------------+--------+
| sales_id | country_id | state_id | city_id | district_id | amount |
+----------+------------+----------+---------+-------------+--------+
| 1 | 1 | 0 | 0 | 0 | 1000 |
| 2 | 1 | 0 | 0 | 0 | 2000 |
| 3 | 1 | 1 | 0 | 0 | 300 |
| 4 | 1 | 1 | 0 | 0 | 70 |
| 5 | 1 | 1 | 1 | 0 | 50 |
| 6 | 1 | 1 | 1 | 1 | 25 |
| 7 | 1 | 4 | 1 | 1 | 25 |
| 8 | 1 | 4 | 5 | 0 | 25 |
| 9 | 2 | 0 | 0 | 0 | 3000 |
| 10 | 2 | 0 | 0 | 0 | 500 |
| 11 | 2 | 3 | 0 | 0 | 300 |
| 12 | 2 | 3 | 4 | 6 | 70 |
+----------+------------+----------+---------+-------------+--------+
Demo with my current attempt
Thank you
If you want to group by three column (city_id, state_id, district_id), it's pointless, since it won't change anything in your sales table. There you have already partitoned information.
Moreover, in sales table you have 0 in place of some IDs and there isn't any ID equal to 0 in any table. You need to rethink what you really want I guess.
I think you just need simple JOIN:
SELECT country,
state,
city,
district,
coalesce(amount, 0) amount
FROM country ctr
JOIN state_province st ON ctr.country_id = st.country_id
JOIN city c ON c.state_id = st.state_id
LEFT JOIN district d ON d.city_id = c.city_id
LEFT JOIN sales s ON
s.country_id = ctr.country_id AND
s.state_id = st.state_id AND
s.city_id = c.city_id AND
s.district_id = d.district_id
Demo

time slot for students and teacher

I am looking to set start time for first 20 students of a particular subject
at 9:00 AM and end time 10:00 AM, for the next 20 i want the time for the same subject 10:00 AM- 11 AM , this should happen for each subject, also i want that only 1 teacher for that particular subject should be alloted for each time slot,
i have tried a lot but still unable to come up with the solution.
user_table
user_id | user_name | contact_no | password | flag
6 | Abhis | 123456788 | 123 | s
5 | Abhish | 123456789 | 123 | s
8 | Sneha | 1111111111 | 123 | s
7 | Snehil | 1111112222 | 123 | s
1 | Narsingh | 1234567890 | 123 | t
2 | Abhinav | 1234567891 | 1234 | t
3 | Abhi | 1234567892 | 123 | s
4 | Abhishek | 1234567893 | 123 | s
subject_table
sub_id | sub_name
3 | CSS
1 | HTML
2 | JQUERY
enr_id | sub_id | user_id | date |
1 | 1 | 1 | 2016-04-01 |
2 | 2 | 1 | 2016-04-01 |
3 | 3 | 1 | 2016-04-01 |
4 | 1 | 5 | 2016-04-01 |
5 | 1 | 6 | 2016-04-01 |
6 | 1 | 7 | 2016-04-01 |
7 | 1 | 2 | 2016-04-01 |
8 | 2 | 2 | 2016-04-01 |

Dynamically create HTML table from sql query result

how can i write the sql query to get rows from multiple MySQL table and then output it to an HTML table as the format below.
I've been trying this for hours and I'm still not getting it.
Below is the HTML structure that I am trying to create
----------------------------------------------------------------
|student_id | full_name | TERM 1 | TERM2 | TERM 3 | TERM 4 |
----------------------------------------------------------------
| 1 | John Doe | 67 | 90 | NA | NA |
| 2 | John Smi | NA | NA | NA | NA |
| 3 | Doe John | 88 | 66 | NA | NA |
| 4 | Mike Doe | 57 | 78 | NA | NA |
| 5 | Doe Mike | NA | NA | NA | NA |
| 6 | Sam Doe | NA | NA | NA | NA |
----------------------------------------------------------------
And Here are the structures for the MySQL tables
--------------------------
| student_id | full_name |
--------------------------
| 1 | John Doe |
| 2 | John Smi |
| 3 | Doe John |
| 4 | Mike Doe |
| 5 | Doe Mike |
| 6 | Sam Doe |
--------------------------
------------------------
| exam_id | exam_name |
------------------------
| 11 | TERM 1 |
| 12 | TERM 2 |
| 13 | TERM 3 |
| 14 | TERM 4 |
------------------------
----------------------------------------
| subject_id | exam_id | subject_name |
----------------------------------------
| 1 | 11 | mathematics |
| 2 | 11 | english |
| 3 | 11 | physics |
| 4 | 12 | mathematics |
| 5 | 12 | english |
| 6 | 12 | physics |
----------------------------------------
---------------------------------------------
| subject_id | marks | student_id |
---------------------------------------------
| 1 | 67 | 1 |
| 2 | 54 | 4 |
| 3 | 88 | 3 |
| 4 | 90 | 1 |
| 5 | 78 | 4 |
| 6 | 66 | 3 |
---------------------------------------------
Thanks.
This is the query you want:
select s.student_id,
s.full_name,
avg(case when te.exam_name = 'TERM 1' then sm.marks else null end) as term_1,
avg(case when te.exam_name = 'TERM 2' then sm.marks else null end) as term_2,
avg(case when te.exam_name = 'TERM 3' then sm.marks else null end) as term_3,
avg(case when te.exam_name = 'TERM 4' then sm.marks else null end) as term_4
from students s
left join subject_marks sm
on s.student_id = sm.student_id
left join subject_exams se
on sm.subject_id = se.subject_id
left join term_exams te
on se.exam_id = te.exam_id
group by s.student_id,
s.full_name
Fiddle: http://sqlfiddle.com/#!2/fd1c82/1/0
As for outputting it to an HTML table, you want to look at a PHP/MySQL tutorial.

Selecting Total value of items from 2 tables and update value of another TABLE (COMPLICATED QUERY)

My case is that I want to compute a player atk power to be used on a battle module that i made, but just wondering i have actually 2 options:
Calculate Damage Dealt from the server.(my CURRENT OPTIONS)
use PHP to calculate DAMAGE DEALT and UPDATE server DATABASE values.
pass 2 of the chara id and just calculate all in the QUERY and UPDATE all(is this possible).
Question: Can I do it in a query?(option B)
my current set-up:
1 character has 4 items and i compute the characters atk by adding all 4 of the item atk and the chara base atk in the client side. (which i think is prone to security holes)
and then update the values in the server side.
Here is my tables:
chara:
+----------+------------+----------------+-------------+------------+----------+----------+----------+-----------+-----------+
| chara_id | chara_name | chara_class_id | chara_level | chara_gold | chara_hp | chara_mp | chara_xp | chara_atk | chara_def |
+----------+------------+----------------+-------------+------------+----------+----------+----------+-----------+-----------+
| 1 | LawrenceX | 1 | 5 | 230 | -175 | 1000 | 0 | 7 | 3 |
| 3 | Viscocent | 2 | 2 | 96 | -206 | 1100 | 1700 | 5 | 5 |
| 4 | Piatos | 1 | 1 | 120 | -60 | 1000 | 0 | 7 | 3 |
| 5 | Hello | 1 | 1 | 300 | -50 | 1000 | 200 | 2 | 8 |
| 6 | Sample | 3 | 2 | 251 | -85 | 900 | 0 | 9 | 1 |
| 8 | Sampuro | 2 | 1 | 170 | 895 | 1100 | 700 | 5 | 5 |
| 12 | fail | 2 | 3 | 481 | 1100 | 1300 | 0 | 21 | 9 |
| 13 | new | 1 | 1 | 1000 | -80 | 1000 | 0 | 5 | 5 |
+----------+------------+----------------+-------------+------------+----------+----------+----------+-----------+-----------+
items:
+---------+-----------------+-----------+----------+----------+----------+---------------------------------+-------------------------------------------------+------------+
| 0 | None | 0 | 0 | 0 | 0 | pics/none.png | | 400 |
| 1 | Axe | 1 | 220 | 10 | 0 | pics/weapons/axe.png | Another lumberjack axe is another man's weapon. | 200 |
| 2 | Wooden Sword | 1 | 70 | 0 | 0 | pics/weapons/wooden-sword.png | A wooden sword, 99% made from wood | 225 |
| 3 | Dagger | 1 | 60 | 5 | 0 | pics/weapons/dagger.png | A Dagger, Cheap and Sharp | 55 |
| 4 | Bow | 1 | 120 | 1 | 0 | pics/weapons/bow.png | The basics and simplest of all bows. | 120 |
| 5 | Helmet | 4 | 0 | 50 | 0 | pics/headgears/helmet.png | iron helmet - made from an iron pot scraps. | 155 |
| 6 | Tunic | 2 | 10 | 10 | 0 | pics/armors/tunic.png | A peasants tunic. | 50 |
| 7 | Armour | 2 | 0 | 75 | 0 | pics/armors/armour.png | | 150 |
| 8 | Necklace | 3 | 25 | 15 | 0 | pics/accessories/necklace.png | | 199 |
| 9 | Studded Leather | 2 | 25 | 60 | 0 | pics/armors/studded-leather.png | | 240 |
+---------+-----------------+-----------+----------+----------+----------+---------------------------------+-------------------------------------------------+------------+
equipment:
+----------+----------+-----------+-------------+----------+---------+
| equip_id | chara_id | weapon_id | headgear_id | armor_id | ring_id |
+----------+----------+-----------+-------------+----------+---------+
| 3 | 1 | 14 | 5 | 6 | 8 |
| 5 | 3 | 4 | 5 | 6 | 8 |
| 6 | 4 | 11 | 5 | 7 | 8 |
| 7 | 5 | 12 | 5 | 6 | 8 |
| 8 | 6 | 3 | 16 | 7 | 8 |
| 10 | 8 | 15 | 5 | 7 | 8 |
| 13 | 12 | 14 | 5 | 6 | 17 |
| 40 | 13 | 3 | 5 | 7 | 8 |
+----------+----------+-----------+-------------+----------+---------+
table relationships:
1 chara = 1 equipment
1 weapon_id, armor_id, ring_id, headgear_id = 1 item (total of 4 items, headgear_id = 1 item).
I CAN GET THE EQUIPMENTS OF A CHARACTER BY USING THIS QUERY(KUDOS #JC):
SELECT i1.item_atk weapon_atk,i1.item_def weapon_def,
i2.item_atk headgear_atk,
i2.item_def headgear_def,
i3.item_atk armor_atk,
i3.item_def armor_def,
i4.item_atk ring_atk,
i4.item_def ring_def
FROM equipment e LEFT JOIN
item i1 ON e.weapon_id = i1.item_id LEFT JOIN
item i2 ON e.headgear_id = i2.item_id LEFT JOIN
item i3 ON e.armor_id = i3.item_id LEFT JOIN
item i4 ON e.ring_id = i4.item_id
WHERE e.chara_id = 1
RESULTS:
+------------+------------+--------------+--------------+-----------+-----------+----------+----------+
| weapon_atk | weapon_def | headgear_atk | headgear_def | armor_atk | armor_def | ring_atk | ring_def |
+------------+------------+--------------+--------------+-----------+-----------+----------+----------+
| 275 | 25 | 0 | 50 | 10 | 10 | 25 | 15 |
+------------+------------+--------------+--------------+-----------+-----------+----------+----------+
now i want to total the atk and def of that character equipment and return it in that query
expected results:
+------------+------------+
| total_atk | total_def |
+------------+------------+
| 310 | 100 |
+------------+------------+
this is the simplest way that I can think of.
SELECT IFNULL(W.item_atk, 0) + IFNULL(H.item_atk, 0) + IFNULL(A.item_atk, 0) + IFNULL(R.item_atk, 0) AS total_atk
, IFNULL(W.item_def, 0) + IFNULL(H.item_def, 0) + IFNULL(A.item_def, 0) + IFNULL(R.item_def, 0) AS total_def
FROM equipment E
LEFT JOIN item W ON W.item_id = E.weapon_id
LEFT JOIN item H ON H.item_id = E.headgear_id
LEFT JOIN item A ON A.item_id = E.armor_id
LEFT JOIN item R ON R.item_id = E.ring_id
WHERE E.chara_id = 1
I have renamed the aliases of tables to track them easily. And I used IFNULL in case the character has no particular equipment.
==================================================================================
Dude, I just made another query, I think this is faster than the one above. Though, I haven't tested them.
SELECT SUM(IFNULL(I.item_atk, 0)) AS total_atk
, SUM(IFNULL(I.item_def, 0)) AS total_def
FROM equipment E
LEFT JOIN item I ON I.item_id = E.weapon_id
OR I.item_id = E.headgear_id
OR I.item_id = E.armor_id
OR I.item_id = E.ring_id
WHERE E.chara_id = 1
GROUP BY E.chara_id

mysql inner join 2 tables and order by count

I am having the following tables in my DB
PROJECTS
+----+-------------------------------------------+
| id | name |
+----+-------------------------------------------+
| 1 | YANNONALI COURT |
| 2 | UNIVERSITY OF COLORARDO DENVER RESEARCH 2 |
| 3 | G.R.E.A.T PROGRAM DESALTER BUILDING |
| 4 | MONARCH CLUB |
| 5 | LAFAYETTE MERCANTILE |
| 6 | CAMELBACK VILLAGE RAQUET AND HEALTH CLUB |
| 7 | BACK COUNTRY |
| 8 | URBAN CRASHPAD |
| 9 | PRIVATE RESIDENCE |
| 10 | EATON RESIDENCE |
+----+-------------------------------------------+
PROJECT_ASSIGNMENTS(WHERE projects.id=project_assignment.target_id)
+-------+-----------+-------------+
| id | target_id | property_id |
+-------+-----------+-------------+
| 19178 | 1 | 48 |
| 19192 | 1 | 39 |
| 19391 | 1 | 3 |
| 19412 | 2 | 3 |
| 19591 | 2 | 34 |
| 19610 | 2 | 34 |
| 21013 | 3 | 2 |
| 21032 | 3 | 2 |
| 30876 | 4 | 2433 |
| 38424 | 5 | 2580 |
+-------+-----------+-------------+
PROPERTIES(WHERE properties.id= project_assignment.property_id)
+----+------------------+
| id | name |
+----+------------------+
| 2 | Residential |
| 3 | Multi Family |
| 34 | New Construction |
| 39 | Contemporary |
| 48 | Southwest |
+----+------------------+
I want O/P ordered by no.of projects in the list...
Residential(177) //12 - total no.of projects which is having this property
Multi Family(15)
New Construction(13)
Contemporary(11)
please give me some MySQL queries
Thank You
This should do the trick:
select
c.name,
count(c.id) as CountOfProperties
from
projects a,
project_assignments b,
properties c
where
a.ID=b.target_id
and b.property_id=c.ID
group by
c.name
order by
count(c.id) desc;
Try this::
select
prop.name,
count(prop.id) as CountOfProperties
from
projects p
inner join project_assignments pa on (p.ID=pa.target_id)
inner join properties prop on (pa.property_id=prop.ID)
group by
prop.name
order by
count(prop.id) desc;

Categories