join two tables and fetch result in two array - php

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
)
)

Related

join query in one sql statement

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

php sort data according to 2nd table

I have two tables: Table 1 and Table 2
I need to sort the data as shown in Table View.
I have already taken:
foreach($Table1 as $row1 )
{
foreach($Table2 as $row2 )
{
if($row1->id == $row2->selected_id)
{
show??
}
}
}
please do not worry about queries: it is taken care of real issue is:
there is only one problem: Table 1 is generated from 20 different function and collects to Table 1. and user selects which they want to view by selecting. so, if there is total of 7 rows and user selects 3, so data will be sorted showing 3 rows selected at first rows and rest after the 3 rows.
is this the right way to do it? or is there much easier way
I am using MySQL
please help anyone?
As per your additional question, it seems that you don't want to use MySQL for that, and that your user selection is in fact an array like this one:
$filter = [ 3, 4, 7, 8 ];
Filter the original $Table1 array by using array_reduce. With this method you obtain an array ($filtered) with user selection in element 0 and other values in element 1:
$filtered = array_reduce
(
$Table1,
function( $carry, $item ) use( $filter )
{
$index = !in_array( $item['id'], $filter );
$carry[$index][] = $item;
return $carry;
},
[ [], [] ]
);
Then, merge $filtered data with call_user_func_array:
$result = call_user_func_array( 'array_merge', $filtered );
Result:
Array
(
[0] => Array
(
[id] => 3
[name] => C
)
[1] => Array
(
[id] => 4
[name] => D
)
[2] => Array
(
[id] => 7
[name] => G
)
[3] => Array
(
[id] => 8
[name] => H
)
[4] => Array
(
[id] => 1
[name] => A
)
[5] => Array
(
[id] => 2
[name] => B
)
[6] => Array
(
[id] => 5
[name] => E
)
[7] => Array
(
[id] => 6
[name] => F
)
)
You can achieve this in a single query, no need to loop over both tables afterwards
Select t1.id, t1.name
From Table1 t1
Left Join Table2 t2 On t1.id = t2.selected_id
Order By t2.id, t1.id
You can do below query.
Select t1.* from table2 t2 inner join table1 t1 on t2.selected_id = t1.id order by t2.id
UNION
Select t1.* from table1 t1 where t1.id NOT IN ( select t2.id from table2 t2 inner join table1 t1 on t2.selected_id = t1.id ) order by t1.id
You can use an effective SQL query to get the final table immediately like so:
Query1:
SELECT t2.id, t1.name
FROM t2
INNER JOIN t1
ON t2.id=t1.id;
This would output the first 4 rows and then:
Query2:
SELECT id, name
FROM t1
WHERE id NOT IN (SELECT id FROM t2);
Would output the last 4 rows, after that just do a UNION:
Final query:
Query1 UNION Query2;
Explicitly:
(SELECT t2.id, t2.name
FROM t2
INNER JOIN t1
ON t2.id=t1.id)
UNION
(SELECT id, name
FROM t1
WHERE id NOT IN (SELECT id FROM t2));
Bit more compact:
(SELECT t2.*
FROM t2
INNER JOIN t1
ON t2.id=t1.id)
UNION
(SELECT *
FROM t1
WHERE id NOT IN (SELECT id FROM t2));
You can use at query : Order By field(). In the field() function second table data as string comma separated.
You can use Union to show the selected rows first then the not selected ones next, combined with (where in ) and (where not in) clauses next here is the query:
(select t1.id, t1.name from table1 t1 where t1.id
in (select t2.selected_id from table2 t2)) union
(select t1.id , t1.name from table1 t1 where t1.id
not in (select t2.selected_id from table2 t2));

Query with 2 selected columns returns only 1 column per row

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

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;

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