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"
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 have my output array named "Feed Items" (fi):
(
[0] => Array
(
[reg] => 2015-08-03 13:39:00
[id] => fd7ec4107d16b07c1a13cbdd386af8d2cb05ffca
[user_id] => de5fd44db1760b006b1909cf1db11a78b38e455c
[img] => edee88e88cf6e17732e393b5433cfd894662902e
[type] => new_join_ambition
)
)
I generate an array named "RemovedFeedItems" (rfi) that has the following structure:
(
[0] => Array
(
[object_id] => fd7ec4107d16b07c1a13cbdd386af8d2cb05ffca
[postee_id] => de5fd44db1760b006b1909cf1db11a78b38e455c
[type] => new_join_ambition
)
)
My mission is to not get the records that have the following condition:
ri.id == rfi.object_id && ri.user_id == rfi.postee_id && ri.type == rfi.type
The query that I use to get the Feed Items array is:
SELECT
i.registered AS reg,
a.id,
u.id AS user_id,
ui.image_id AS img,
'new_join_ambition' AS type,
FROM x_ambition_invites i
LEFT JOIN x_user u
ON i.to = u.id
LEFT JOIN x_user_images ui
ON u.id = ui.user_id
LEFT JOIN x_ambitions a
ON i.ambition_id = a.id
WHERE a.registered
BETWEEN '2014-07-21 14:25:03' AND '2015-08-05 12:04:41'
AND i.to != '8fa7a1679560876eaf2f8060abd916b692c719dc'
AND i.to IN ('de5fd44db1760b006b1909cf1db11a78b38e455c')
How can I adapt my query to implement the condition to remove the stated record from the FeedItems Array?
Thanks in advance.
Try with an inline table:
SELECT ... FROM ... WHERE (a,b,c) NOT IN (SELECT a1, b1, c1 UNION SELECT a2, b2, c2 UNION SELECT...)
The drawback is that you'll have to build a large query string. :-(
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
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;
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