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;
Related
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
I am running the following query.
I expect to get both user.name and activities.name for every row in my result, but I only seem to get activities.name.
Why am I not seeing the names of each user that did the actual activity?
SELECT user.name, activities.name
FROM user_activities
LEFT JOIN user ON user.userid = user_activities.user_id
LEFT JOIN activities ON activities.activityid = user_activities.activity_id
Ouput:
stdClass Object
(
[0] => Array
(
[name] => Bought a House.
)
[1] => Array
(
[name] => Purchased a game.
)
[2] => Array
(
[name] => Purchased a game.
)
)
Since they share the column name, try to add an alias for that column:
SELECT user.name AS user_name , activities.name AS activity_name
If I'm not wrong to understand your question you might need to use alias to identify names of a user that'll result into non-ambiguous fields. Just Update your query like as below it'll result as you desired
SELECT u.name as user_name, a.name as activites_name FROM user_activities ua LEFT JOIN user u ON
u.userid = ua.user_id LEFT JOIN activities a ON a.activityid
= ua.activity_id
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.
I am trying to build my database using related tables. I am getting the right output from the database, however - Since the user is the same, and the only change in data is the courses, I would like to gather the values "Engelsk" and "Matematik" in the same row, instead of having two outputs which are virtually the same, except the courses.
Is this even possible without having this in the same row in the database? And if so, I'd very much like to know how :)
Array
(
[0] => Array
(
[Type] => Elev
[Username] => test
[Name] => Test Testsen
[Grade] => 9. Klasse
[Course] => Engelsk
)
[1] => Array
(
[Type] => Elev
[Username] => test
[Name] => Test Testsen
[Grade] => 9. Klasse
[Course] => Matematik
)
)
So basically what I would like to achieve is something like this:
Array
(
[0] => Array
(
[Type] => Elev
[Username] => test
[Name] => Test Testsen
[Grade] => 9. Klasse
[Course] => Engelsk, Matematik
)
My query looks like this:
SELECT
*
FROM
lek_Essentials
LEFT JOIN
lek_Type
ON
lek_Essentials.TypeId = lek_Type.TypeId
LEFT JOIN
lek_Grades
ON
lek_Essentials.GradeId = lek_Grades.GradeId
LEFT JOIN
lek_GradeCourses
ON
lek_Grades.GradeId = lek_GradeCourses.GradeId
LEFT JOIN
lek_Courses
ON
lek_GradeCourses.CourseId = lek_Courses.CourseId
LEFT JOIN
lek_Request
ON
lek_Courses.CourseId = lek_Request.CourseId
WHERE
lek_Essentials.UserId = lek_Request.UserId
It's not exactly clear what tables each of the columns in your array are coming from but in MySQL you can use GROUP_CONCAT to aggregate the two rows into one:
SELECT t.type,
e.username,
e.name,
g.grade,
group_concat(c.course) course
FROM lek_Essentials e
LEFT JOIN lek_Type t
ON e.TypeId = t.TypeId
LEFT JOIN lek_Grades g
ON e.GradeId = g.GradeId
LEFT JOIN lek_GradeCourses gc
ON g.GradeId = gc.GradeId
LEFT JOIN lek_Courses c
ON gc.CourseId = c.CourseId
LEFT JOIN lek_Request r
ON c.CourseId = r.CourseId
WHERE e.UserId = r.UserId
group by t.type, e.username, e.name, g.grade
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"