JOIN with 2 Tables (Voting) Mysql, PHP - php

Hello I have 3 Tables but i need only help with 2 Tables.
The first Table is champions. In this Table are over 100 names.
The second Table is champion_names there are nicknames for the first Table.
The third Table is champion_names_vote. There are the votes for the nicknames. Thumb up if I like any nickname or thumb down.
On my website I have a site where I can see a list full of Names (Table 1). There are 2 columns. In the first is the normal name (Table 1) in the second the nickname (Table 2). Now I want to show the best nickname in column 2. Actually it's random but I only want to show the best nickname.
I can show all Names that's not the problem. But if I want to show only the best nicknames I don't know how.
Table 2: id(AI), champ_id(this is the id for Table 1), sender_id, name
Table 3: id(AI), userid, name_id(Table 2 ID), like_dislike
like_dislike = 1 is like, -1 is dislike and 0 is nothing.
Example:
Table 2: 50, 2, 4, Test
Table 3: 1, 3, 50, 1,
I liked the name of Table 2. So the name_id in Table 3 is the id of Table 2
So how can I do this with JOIN?
Can you help me please.

something like this should do the trick.
Idea is to Analyse your votes per nickname first. Then select the Maximum vote-score by HAVING:
SELECT voteResult.name, vote as winnerVoteScore
FROM(
select a.name_id, b.name,
sum(IF(a.like_dislike = 1, 1, IF(a.like_dislike = 2, -1, 0 ))) as vote
FROM champion_names_vote as a JOIN champion_names as b
ON a.name_id = b.ID
GROUP BY 1
) as voteResult
GROUP BY 1
HAVING vote = max(vote)

Related

Php Mysql sum over 2 colums and select from 2 tables

I have a MySQL database with 3 tables.
Table player
P_id = content (1, 2, 3,)
P_name = content (Spieler1name, Spieler2name, Spieler3name)
Table tournament
T_id = content (1, 2)
T_date = content (2018, 2019)
T_players = content (123, 123)
Table Games
G_id = content (1, 2)
G_tournament_id = content (T_id from table tournament)
G_player_away = Content (as an example user id 1)
G_player_home = content (as an example user id 2)
G_player_away_points = Content (11)
G_player_home_points = Content (8)
The Games table has even more data.
Now I want to create a table with PhP as ranking.
Place name points
Spieler1name 11
Spieler2name 8
Spieler3name 4
....
Can someone tell me how the php query from the mysql database works? I tested this but it doesn't work.
SELECT SUM(G_player_away_points) AS total FROM Games
UNION
SELECT SUM(G_player_home_points) AS total FROM Games
GROUP BY G_player_away
In the code above, he also sums the points from the opponent. This is of course not correct, it should only the points obtained by him appear in the output with php table. Furthermore you have to get the name from the table players. Can someone help me?
If you are using single table and multiple column for sum then you should try with this
SELECT SUM(G_player_away_points + G_player_home_points) AS total FROM Games
GROUP BY G_player_away

Convert Received Data from SQL and Translate

I am wanting to convert data that is queried from the database to another string. The best way I can explain what I'm trying to do is below:
I have a table called "users". Inside that table I have a column called "rank". Rank is a two-digit integer ranging from 1 to 21.
I want to convert that "1" or whichever number into more understandable text in my php file. So if your rank is set to "1", the output is "User" or what-have-you.
How do I go about doing this? I'm not sure what this process is called. I'm very new to php.
Thank you!
Create a rank table having 2 columns, id and rank:
id | rank
-------------
1 | user
2 | admin
3 | goof
Then JOIN the users table with the rank table:
SELECT u.name, r.rank
FROM users u
LEFT JOIN rank r
ON u.rank = r.id
You can store the ranks title in another table like mentioned by Jay Blanchard or you can define an array with the rank titles like this
$ranks = array(
1 => 'User',
2 => 'Administrator',
3 => 'Moderator'
);
and can echo like this
echo $ranks['rank_id_fetched_using_sql'];
rank_id_fetched_using_sql is supposed to be 1 to 21

Select from three tables

I have three tables where table_2 is the middle(connected) between table_1 and table_3
tables
table_id
...
...
table_rest
rest_id
table_id
...
rest
rest_id
...
...
And the query to select I use
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
WHERE rest_id = '$rest_id'
What I need now is to join in this query another table reserv
id
...
status
To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.
EDIT: Sample scenario
tables
table_id
1
2
3
4
5
rest
rest_id
1
2
table_rest
table_id | rest_id
1 2
2 2
3 2
4 2
5 2
So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1
So now I have another table
reserv
id | status
1 0
2 1
3 2
So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.
Hope is a little bit more clear now.
You have to point from table reserv to which table is beign booked, let's call it reserv.table_id
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
left join reserv
on reserv.table_id = m.id
WHERE rest_id = '$rest_id'
and reserv.status is null (*note)
*note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =
It's better if you provide sample data or sqlfiddle. Based on what I realize: Is this what you want:
select tables.table_id, rest.rest_id
from tables
left join table_rest on table_rest.table_id = tables.table_id
left join rest on rest.rest_id = table_rest.rest_id
where rest.rest_id = '$rest_id'
and tables.table_id not in (select id from reserv)

MySQL SELECT from array of ids where one is mandatory

I'd like to know the most efficient SQL query for achieving this problem:
Say we have a table with two columns, one storing entry ids (entry_id) and one storing category ids (cat_id):
entry_id cat_id
3 1
3 2
3 3
3 20
4 1
4 2
4 21
I'd like to count how many distinct entry_id's there are in the categories 1, 2 OR 3 but that also must be in cat_id 20.
For example, categories 1, 2 and 3 might represent music genres (Country, Pop etc.), while category 20 might be recording formats (CD, Vinyl etc.). So another way of putting it verbally could be: "How many products are there that are on Vinyl and in either the Pop or Country category?"
I could achieve this with a nested loop in code (PHP) or possibly with a nested SQL subquery, but neither feels that efficient. I feel there must be an obvious answer to this staring me in the face...
EDIT TO ADD:
I would also like to do this without modifying the database design, as it's a third party system.
FURTHER EXAMPLE TO CLARIFY:
Another real-world example of why I'd need this data:
Let's say the category ids instead represent either:
Accommodation Types (Camping = 20, Holiday Cottage = 21)
OR
Continents and their sub-regions (i.e. Europe = 1, UK = 2, England = 3)
Let's say someone has selected that they are interested in camping (cat_id = 1). Now we need to count how many camping products there are in the Europe. A product might be tagged as both Europe (parent), UK (child) AND England (grand-child), giving us an array of category ids 1, 2 or 3. So we now need to count how many distinct products there are in both those categories AND the original accommodation category of 1 (camping).
So having selected Camping, the end result might look something like:
Europe: 4 camping products
UK: 2 camping products
England : 1 camping product
Wales : 1 camping product
France: 2 camping products
etc.
Hope that helps...
I believe you want GROUP BY, COUNT() and EXISTS()
declare #t table(entry_id int, cat_id int)
insert #t select 1, 1
insert #t select 2, 1
insert #t select 1, 2
insert #t select 2, 2
insert #t select 3, 1
insert #t select 1, 20
select t1.cat_id, COUNT(*)
from #t as t1
where exists(
select * from #t
where t1.entry_id = entry_id
and cat_id = 20)
group by t1.cat_id
V2 using join instead of EXISTS()
declare #t table(entry_id int, cat_id int)
insert #t select 1, 1
insert #t select 2, 1
insert #t select 1, 2
insert #t select 2, 2
insert #t select 3, 1
insert #t select 1, 20
select t1.cat_id, COUNT(*)
from #t as t1
join #t as t2 on t1.entry_id = t2.entry_id and t2.cat_id = 20
group by t1.cat_id
select count(distinct entry_id) from myTable where cat_id=20 and entry_id in
(select distinct entry_id from myTable where cat_id in (1,2,3));
With no subqueries, using JOIN and GROUP BY:
Join the table to itself using entry_id (this gives you all possible pairs of cat_id for that entry_id). Select rows having cat_id both a member of (1,2,3) and the second cat_id = 20.
SELECT r1.entry_id
FROM records r1
JOIN records r2 USING(entry_id)
WHERE r1.cat_id IN (1,2,3)
AND r2.cat_id = 20 GROUP BY entry_id;

showing a grandtotal of values in a field

good pm. i was thinking is it possible to show the summation or grand total of a selected field in the table and with relation to date:
for example is i want to know the total beer consumption of my hotel every month.
i have here my table on services:
[services_id[pk],
customer_id[fk],
date_in,date_out,room_type,room_number,
extra_ref,
extra_bed,extra_snack,
extra_beer,extra_softdrinks,
extra_pillows,extra_breakfast,
extra_snack_q,
extra_beer_q,
extra_softdrinks_q,
extra_pillows_q,
extra_breakfast_q]
can you give some advice on how can i get it.
thanks in advance:
-renz
SELECT SUM(beer_amount) as monthly_beer_amount
FROM [DATABASE].[TABLE]
WHERE beer_sold_date BETWEEN '20110201' AND '20110228'
[EXTRA INFO]
Also I believe that the best way to organize this table is to separate out this table into a few other tables. Store customer info in the first table such as
[customer_id, customer_name, date_in, date_out, room_type, room_number].
1, Bob, 20110101, 20110110, big, 200
2, Joe, 20110101, 20110110, small, 202
....
And have another table named something like room_items which would have the following,
[id, item_name]
1, BEER
2, BED
3, SNACK
...
And then another table named room_purchases which will have the following,
[customer_id, purchase_id, amount, date....]
1, 1, 10, 20110101
2, 3, 5, 20110101
3, 1, 9, 20110101
....
This would help you to do a join on all three tables and they would be more normalized in this way.
SELECT SUM(t2.amount) as beer_amount
FROM customers as t1
LEFT JOIN room_purchases as t2 ON t1.customer_id = t2.customer_id
WHERE t2.purchase_id = 1 AND t2.date BETWEEN 20110101 AND 20110131
SELECT DATE_FORMAT(date_in,"%m-%Y") AS month,SUM(extra_beer) AS beers
FROM your_table
GROUP BY month
this will return something like this:
02-2011 | 43
03-2011 | 52
if you want to limit the query depending on the date just add a WHERE constraint before the GROUP BY

Categories