How To Merge Array Data From Database - php

I have a database table for a user of my website, this table gives each user a user_id.
Using normalization, I have linked the user to a group with a user_group table including user_id and group_id to link a user to a group.
I then have a group table that links a group name to the group_id.
I am trying to output the users on my webpage in a list next to the name of the group, not its id.
I was thinking of using a foreach loop to do this, but the data would need to get into one array? I dont know how I would take the user_id, find out which group _id it it paired with, find out which group name that id was paired with, and then add that to the array with the names and be able to display the group name next to the user's name using foreach.
Thanks for any help.

If i understand you correctly, you want to join you users data to group to show the user details with their group name.
Try out this:
select u.*,g.*,ug.* from users u
left join user_group ug on u.user_id=ug.user_id
left join `group` g on ug.group_id=g.group_id;
From above query you may get blank values for some user's, who are not assigned to any group.

I don't see why a left join is necessary. Should be a regular join unless he wants to display users that don't have groups.
By the way, this is the perfect example of something that should be done in the DB with a select. Pulling everything from these tables and trying to sort through them in java is not a good idea. Maybe I read this post wrong though.

Related

MySQL join 2 tables?

I have a database with users. Now I'm trying to create a wall (Facebook like).
I've added a table in my db called status_update and added a row username(user who posted the status). Then I created a query to search for statuses from the user on whose profile page you're on (select from status where username=$_GET['profile']).
I'm wondering how smart this is? Is there a way to use JOIN or something? Now this was the only logic solution that came to my mind and I know how to make it.
Also this is fine for this page but when I'll create a feed page for those who are "Friends or Followers" that's gonna be a bit tricky.
Is there a better solution for this?
yes you can join two tables by there index or any value, just do it like this
select * from table1 inner join table2 on table.field=table.field
it depends what join you want, inner join if data present on both table other wise left if data may or may not present

Many to many relation, IN operator and possibility of improper results

I have a problem with creating optimal SQL query. I have private messages system where user can send single message to many of users or groups of users. Recipients are stored in single text column (don't ask me why is that I wasn't responsible for designing that) like that:
[60,63,103,68]
Additionaly I've added new text column where is placed group which user belongs to so I can have as a groups in database:
[55,11,11,0]
Now I want to get all users (receivers) and their groups. I have table where relation between user and group id. The problem is that single user can belong to multiple groups, for example user 60 can be in group ID 55 and 11. I would like to do it in the most optimal way (there can be 50+ receivers stored in column...) so I can write query like that:
SELECT u.name, u.last_name, g.group_name
FROM
user u
LEFT JOIN
group g ON u.id = g.user_id
WHERE
u.id IN (".$users.") and
g.id IN (".$groups.")
Unfortunately group name returned by query might by not proper - connected with the group ID i placed in WHERE. I may create PHP foreach and get user and his group using IDs I have:
foreach($user as $key => $single)
{
$sql = "...
where u.id = $single AND g.id = $group[$key] ";
}
but I think this is very bad way. Is there any way to get user and specified group in single query?
Since users and groups are only linked by their ordinal positions in the list, you need to make use of that.
The quick and dirty method would be to unnest() in parallel:
SELECT u.name, u.last_name, g.group_name
FROM (
SELECT unnest(string_to_array('3,1,2', ',')::int[]) AS usr_id -- users
, unnest(string_to_array('10,11,12', ',')::int[]) AS grp_id -- groups
) sel
JOIN usr_grp ug USING (usr_id, grp_id)
JOIN usr u USING (usr_id)
JOIN grp g USING (grp_id);
Note how I replaced SQL key words like user or group as identifiers.
-> SQLfiddle
This way, elements with the same ordinal positions in the array (converted from a comma-separated list) form a row. Both arrays need to have the same number of elements or the operation will result in a Cartesian product instead. That should be the case here, according to your description. Add code to verify if that condition might be violated.
Cleaner alternatives
While the above works reliably, it is a non-standard Postgres feature of SRF (set returning functions) which is frowned upon by some.
There are cleaner ways to do it. And the upcoming version 9.4 of Postgres will ship a new feature: WITH ORDINALITY, allowing for much cleaner code. This related answer demonstrates both:
PostgreSQL unnest() with element number

Reverse effect of JOIN

I got two tables :
A list of people;
A list of people I want to ignore.
When I read the list of people, I don't want to see the ignored people in the list.
My current solution is to query a second time the database (to select the people I want to ignore) and remove them from the array I create with PHP. It's working and it's fine.
However, I want to do that in MySQL. I know JOIN will join only if the row exists in the other table. I am looking for something different (won't show the entry IF the row exists).
I have searched in Google but the lack of "keywords" for this gave me no results.
Thanks
SELECT * FROM Person
LEFT OUTER JOIN IgnoredPerson
ON Person.id = IgnoredPerson.id
WHERE IgnoredPerson.id IS null
Explanation:
Exclude the records we don't want from the right side via a where clause
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Without knowing your schema, I'd suggest something along these lines:
SELECT * FROM people WHERE id NOT IN (SELECT person_id FROM ignored_people)
You could try something like this
SELECT * FROM people p WHERE NOT EXISTS (SELECT i.id FROM ignorePeople i where p.id = i.id )
here's a link about EXISTS in MySql

Mysql join query retrieving profile images of users with ids from other table

I'm having trouble with a join query, my issue is as follows.
Table: battles
Fields: id,attacker_id,defender_id
Table: users
Fields: id,profile_image
I would like to do a query to retrieve a battle and get the profile images as well from the other table.
Is there a way to do this in a single or do I have to do more than one?
Thanks in advance.
I wanted to wait a while to see if you had any attempt or if you will answer my first question to know if I understood the problem. But maybe you don't have a starting point. Try something like:
SELECT
a.profile_image as attacker_profile_image,
d.profile_image as defender_profile_image
FROM
`battles` b
LEFT JOIN
`users` a
ON
b.`attacker_id` = a.`id`
LEFT JOIN
`users` d
ON
b.`defender_id` = d.`id`
the problem here is the fact that you need to join with the users table twice, so you will need to create aliases for the columns you plan to use
This query will fetch the two images only, you will need to add the extra fields

Mysql relationships and getting linked data

I have been doing this for a while now, via some php, first lets say we have two tables:
Users
user_id name email
Images
image_id user_id url
user_id and user_id from images table would be linked with a relationship.
Now what I would do is select the user by their Id, check if the user is found, if so then make another query to images table, and check for num rows and loop through the return, is there a function that I could use that would allow me to just select the user and all the images that are linked to the user without doing a joint query.
Thank you for any help
When you say "without doing a joint query" I think you mean "without doing two queries."
In fact, what you want is probably a LEFT JOIN. The idea is that you select users from the user table matching some ID, and LEFT JOIN the images table. The left join will give you null values if no images exist for the user. If you use a normal join, the fact that no matching records exist in the images table will result in no rows returned.
Here is an example:
SELECT u.name, u.email, i.url
FROM Users u
LEFT JOIN Images i ON (i.user_id = u.user_id)
WHERE u.id = #SpecificUserID;
Assuming the user id is found and there are some images for that user, you will get a result that looks like this:
name email url
----- ----- -----
John j#a.com abc.jpg
John j#a.com def.jpg
John j#a.com ghi.jpg
Now as you can see, the name and email values keep repeating. You get a unique image url for each row and the matching username and email.
If you only select one user at a time, this is simple to process in a loop. On the first iteration read all three values. On subsequent iterations just read the url, adding it to your list or array.
Here is a useful tutorial on joins: Understanding JOINs in MySQL and Other Relational Databases
This can be done in one query rather than two by using an inner join to get your result set.
$sql = "SELECT u.user_nid, i.url
FROM tbl_user u
INNER JOIN i.user_nid = u.user_nid
WHERE user_nid = ?"
With this query you will receive a list of the users images and if there are no images returned or the user does not exist, than you will have a row return of zero.
You'll have to use a join to retrieve data from multiple tables in a single query.
The foreign key relationships enforce constraints. Ex: You can't insert a record into Table A referring to a key in Table B without the record actually being in Table B.

Categories