join query in one sql statement - php

This is my query result where one doesn't have group name. I want to get a group name by using parent_id.
Array
(
[0] => Array
(
[groupName] => pizza
[Parent_ID] =>
)
[1] => Array
(
[groupName] =>
[Parent_ID] => 63
)
}
Here is my table structure
Category
1.id
2.parent_id (if id got sub category than id's value enter into parent_id field)
Category Group
1.id
2.name
Join table
1.id
2.cat_id
3.group name
please help me
here is my sql statement
$sql1 = "SELECT gn.group_name AS groupName,
c.parent_id AS Parent_ID
FROM
LEFT JOIN res_category c ON c.id=p.category_id
LEFT JOIN res_category sc ON c.parent_id=sc.id
LEFT JOIN res_cat_category_group cgn ON c.id=cgn.category_id
LEFT JOIN res_category_group gn ON gn.id=cgn.cat_group_id ";

You mean like this?
select * from category a, category_group b, join_table c where c.cat_id = a.id and c.group_name = b.name

Related

join two tables and fetch result in two array

I have two tables and make join by groupid:
$UserInfo= db::Query("SELECT g.* AS GroupPer ,u.* As Userinfo ,
(SELECT COUNT(m.id) FROM ".DATABASE_TP_PREFIX."comments m WHERE m.userid =u.userid ) AS totalcomments
FROM ".DATABASE_TP_PREFIX."user u LEFT JOIN ".DATABASE_TP_PREFIX."userprofile p ON u.userid=p.parentid
LEFT JOIN ".DATABASE_TP_PREFIX."groups g ON g.id=u.groupid
WHERE u.userid=".$Userid."");
I need fetch results as two array (the above result one array)
Forexample :
$this->Info = db::fetch_array($UserInfo,'assoc'); // user info
$this->Permission = db::fetch_array($UserInfo,'assoc'); // user Permission
like this
array (
array[0] (
username => Manour,
email => Eimaidwra#zzzz.com
)
array[1] (
group_tite => Administer,
permissonaccess => 1
)
)

Select filtered sums with joins MySQL and PHP

I am developing a bets system and basically there is a set of matches (or games). The matches always has 2 teams on which the users can bet.
I have the following DB structure:
matches:
teams:
bets:
What I am trying to do with my query is bring from the database the matches and the bets's amounts sum, related to each team, in each match and list all the matches with all the information related to it.
The far I was:
$query="SELECT
a.*,
SUM(b.amount) AS sumA,
SUM(c.amount) AS sumB,
d.name AS teamNameA,
e.name AS teamNameB
FROM matches AS a
LEFT JOIN bets AS b ON(a.teamA = b.team_id AND a.id = b.match_id)
LEFT JOIN bets AS c ON(a.teamB = c.team_id AND a.id = c.match_id)
LEFT JOIN teams AS d ON(a.teamA = d.id)
LEFT JOIN teams AS e ON(a.teamB = e.id) GROUP BY id"
$result = mysql_query($query);
$resultArray = array();
while($row = mysql_fetch_assoc($result)){
$resultArray[] = $row;
}
print_r($resultArray);
exit();
What is printed:
Array
(
[0] => Array
(
[id] => 1
[teamA] => 1
[teamB] => 2
[teamNameA] => "Team 1"
[teamNameB] => "Team 2"
[sumA] => 400
[sumB] => 200
)
[1] => Array
(
[id] => 1
[teamA] => 1
[teamB] => 2
[teamNameA] => "Team 1"
[teamNameB] => "Team 2"
[sumA] =>
[sumB] =>
)
[2] => Array
(
[id] => 1
[teamA] => 1
[teamB] => 2
[teamNameA] => "Team 1"
[teamNameB] => "Team 2"
[sumA] =>
[sumB] =>
)
)
It is almost perfect, but the amounts in "sumA" and "sumB" itens of the first row is wrong. "sumA" should be equal 200 and "sumB" should be equal 100. Of course it is duplicating the sum process. I tried to use the "DISTINCT" statement within the SUM's but them, it eliminates the amount's equal values and I get 100 for "sumA" and 50 for "sumB", what still wrong as well.
I created a solution for you that includes sum(amount), ID of the team, and the team Name.
SELECT SUM(amount), bets.id, teams.name from bets
INNER JOIN teams ON bets.team_id = teams.id GROUP BY teams.id;
It is not the same as what PHP printed, but I find it more useful.
You might as well look at this SQLFiddle I wrote.
Good luck.
http://sqlfiddle.com/#!2/7ffc6/17
try this sql
SELECT A.id as match_id, A.teamA as teamA, A.teamB as teamB,
(SELECT B.name FROM teams B WHERE B.id=A.teamA LIMIT 1) AS teamNameA,
(SELECT C.name FROM teams C WHERE C.id=A.teamB LIMIT 1) AS teamNameB,
(SELECT sum(D.amount) FROM bets D WHERE D.match_id=A.id AND D.team_id=A.teamA) as sumA ,
(SELECT sum(E.amount) FROM bets E WHERE E.match_id=A.id AND E.team_id=A.teamB) as sumB
FROM `matches` A
this is too lengthy and get some time to execute but you will get output according to your choice
Using the hint of #SKRocks, I finally found the correct solution, substituting the bets table's LEFT JOINs for subqueries as following:
$query="SELECT
a.*,
(SELECT sum(D.ammount) FROM bets AS D WHERE D.match_id=a.id AND D.team_id=a.teamA) AS sumA,
(SELECT sum(E.ammount) FROM bets AS E WHERE E.match_id=a.id AND E.team_id=a.teamB) AS sumB,
d.name AS teamNameA,
e.name AS teamNameB
FROM matches AS a
LEFT JOIN teams AS d ON(a.teamA = d.id)
LEFT JOIN teams AS e ON(a.teamB = e.id) GROUP BY id";
Now I get the correct results and maintained the LEFT JOINS for performance. I am choosing this answer as the final solution, because it is what I am actually using. But you gave me the right hint to build this solution.

Mysql inner join returning repeated results

Im trying to pull a character from a mysql database. The character table has 6 columns that link to a foreign item id from the item table, i need to link each item to get the item id, name, and foreign image id, then i need to link that foregin image id to my image table. and pull the image url. The code i coded below to do this is giving me duplicate image urls. Does anyone know whats wrong with it?
This is my results. The image urls are repeating themselfs. I took project_users out to test it and the same thing happened its not being used now but will be used in the future.
I made a sqlfiddle but it looks like its working correct here http://sqlfiddle.com/#!2/7896e/8
Array ( [name] => test1241 [0] => test1241 [gender] => [1] => [left_arm] => Images/Items/leftArm.png [2] =>
Images/Items/leftArm.png [legs] => Images/Items/legs.png [3] => Images/Items/legs.png [torso] =>
Images/Items/torso.png [4] => Images/Items/torso.png [head] => Images/Items/head.png [5] => Images/Items/head.png [hair] => Images/Items/hair.png [6] => Images/Items/hair.png [right_arm] => Images/Items/rightArm.png [7] => Images/Items/rightArm.png )
$sql = "SELECT
pc.project_characters_name as name,
pc.project_characters_gender as gender,
pia1.project_images_url as left_arm,
pia2.project_images_url as legs,
pia3.project_images_url as torso,
pia4.project_images_url as head,
pia5.project_images_url as hair,
pia6.project_images_url as right_arm
FROM project_characters AS pc
INNER JOIN project_users AS pu ON pc.fk_project_users_id = pu.project_users_id
INNER JOIN project_items AS pi1 ON pc.project_characters_left_arm = pi1.project_items_id
INNER JOIN project_items AS pi2 ON pc.project_characters_legs = pi2.project_items_id
INNER JOIN project_items AS pi3 ON pc.project_characters_torso = pi3.project_items_id
INNER JOIN project_items AS pi4 ON pc.project_characters_head = pi4.project_items_id
INNER JOIN project_items AS pi5 ON pc.project_characters_hair = pi5.project_items_id
INNER JOIN project_items AS pi6 ON pc.project_characters_right_arm = pi6.project_items_id
INNER JOIN project_images AS pia1 ON pi1.fk_project_images_id = pia1.project_images_id
INNER JOIN project_images AS pia2 ON pi2.fk_project_images_id = pia2.project_images_id
INNER JOIN project_images AS pia3 ON pi3.fk_project_images_id = pia3.project_images_id
INNER JOIN project_images AS pia4 ON pi4.fk_project_images_id = pia4.project_images_id
INNER JOIN project_images AS pia5 ON pi5.fk_project_images_id = pia5.project_images_id
INNER JOIN project_images AS pia6 ON pi6.fk_project_images_id = pia6.project_images_id
WHERE pc.project_characters_name=:name LIMIT 1";
You can try using the keyword DISTINCT
to select only distinct result.
Example :
SELECT DISTINCT yourField FROM tables;

MySQL secondary query is too performance intensive

I have a query where I want to pull in ID's from another table based on the ID of the item selected in another table. I'm currently doing this with an additional query based on the results that I get from the main query. It's resulting in many many additional queries. Is there a way to condense this into 1 query?
SELECT music.id,
SUM(linked_tags.weight) AS total_weight
FROM (music)
INNER JOIN linked_tags ON linked_tags.track_id = music.id
AND linked_tags.tag_id IN (7,56,59)
GROUP BY music.id
ORDER BY total_weight DESC
Then the additional query comes from running the results from the main query through a foreach loop, where 2713 is the ID of an item in the music table.
SELECT tag_id,
weight
FROM (linked_tags)
JOIN tags_en ON tags_en.id = linked_tags.tag_id
WHERE track_id = '2713'
This results in this object, where all_tags is the data that comes from the 2nd query:
[id] => 1500
[name] => Some Track Name
[total_weight] => 10
[all_tags] => Array
(
[0] => 20
[1] => 28
[2] => 4
[3] => 13
[4] => 16
[5] => 7
[6] => 42
[7] => 56
[8] => 61
)
Is there a way to pull this all into 1 query?
You can combine them directly using join:
select tag_id, weight
from (SELECT music.id,
SUM(linked_tags.weight) AS total_weight
FROM music join
linked_tags
ON linked_tags.track_id = music.id AND linked_tags.tag_id IN (7,56,59)
GROUP BY music.id
) m join
linked_tags
on m.id = linked_tags.track_id join
tags_en
ON tags_en.id = linked_tags.tag_id;
EDIT:
If I understand the query correctly, you are trying to get all tags on "tracks" (or "music") that have one or more tags in the set of (7,56,59). And, you want to get the sum of the weights of those three tags.
You can do this in one pass, if you don't mind have the tags in a comma-delimited list:
SELECT m.id,
SUM(case when lt.tag_id IN (7,56,59) then lt.weight end) AS total_weight,
sum(lt.tag_id IN (7, 56, 59)) as NumSpecialTags,
group_concat(lt.tag_id) as AllTags
FROM music m join
linked_tags lt
ON lt.track_id = m.id
GROUP BY m.id
having NumSpecialTags > 0
order by total_weight desc;
You then have to parse the AllTags list at the application layer.

SQL left join part of the array is empty

My problem is the following code the comment&rating area of the array should have a comment in it as the DB history table does ...?
SELECT users.uid, users.username, history.user_id, history.comment, history.rating
FROM history
LEFT JOIN users
ON users.uid=history.user_id
WHERE history.book_id="$bid"
It returns :
Array ( [uid] => 3 [username] => Reign [user_id] => 3 [comment] => [rating] => )
Then you want to use an INNER JOIN. A LEFT JOIN will return NULL values.
SELECT users.uid, users.username, history.user_id, history.comment, history.rating
FROM history
INNER JOIN users
ON users.uid=history.user_id
WHERE history.book_id="$bid"

Categories