MySQL join 2 tables? - php

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

Related

How To Merge Array Data From Database

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.

query results from 4 different tables with only 1 known value

right now im working on a query and for my brain this is an overload since i just started php and mysql.
I have 4 tables "Seminar, subscription, unsubscription, user" and i need a little data from all of them.
"Seminar, title", "subscription, active, datetime", "user, firstname, lastname" and "unsubscription, reason" Now I have only 1 value that i can use to get all this data and this is the seminar ID now i was wondering if that is enough to get the data from all tables.
Im really lost I hope someone can help me through this mess.
Thanks in advance
I got a little knowledge from inner joins, left joins etc.
Here is my ERD
http://img594.imageshack.us/img594/8923/erdk.png
How does this look? Typed on ipad so sorry for typos.
Select
Sem.title,
Sub.active,
Sub.datetime,
U.firstname,
U.lastname,
Unsub.reason
From
seminar sem
Inner join Subscription sub on sub.seminar_id = sem.id
Inner join user u on sub.user_id = u.id
Left join unsubscription unsub on sub.Id = unsub.subscription_id
Where
Sem.id = your-search-value-here
I'm assuming there won't always be an unsubscription, so this is a left join on to that table.
All others are inner... Meaning a subscription isn't complete without a user AND a seminar.
But If you want details about a seminar, regardless of if a user is subscribed, then your join to user should be changed from INNER to LEFT too.
And If a seminar can exist without ANY subscriptions, and you want such seminars included in the results, you should also change the join on subscriptions to be LEFT.
Note that Any fields selected from a left joined table, where no result was acquired on that table for that seminar, will be null.

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

PHP MySql query 2 tables that have no common attributes at the same time?

I am trying to query 2 tables in a database, each query having nothing to do with each other, other then being on the same page.
Query 1 - The first query on the page will retrieve text and images that are found throughout the page from Table A.
Query 2 - The second query will retrieve several products with a image, description and title for each product from Table B.
I know that putting the second query inside the first query's while loop would work but of course is very inefficient.
How can I and what is the best way to retrieve all the data I need through 1 query?
Thanks,
Dane
So all you want to know is if its ok to have 2 queries on the same webpage? Its A-OK. Go right ahead. Its completelly normal. No one expects a join between table news and table products. Its normal to usetwo queries to fetch data from two unrelated tables.
Use LEFT or INNER JOIN (depends on whether you want to display records from TableA that have no correspondent records in TableB)
SELECT a.*, b.*
FROM TableA a
[LEFT or INNER] JOIN TableB b ON (b.a_id = a.id)
If there's no way to relate the two tables to each other, then you can't use a JOIN to grab records from both. You COULD use a UNION query, but that presumes that you can match up fields from each table, as a UNION requires you to select the same number/type of fields from each table.
SELECT 'pageinfo' AS sourcetable, page.id, page.images, page.this, page.that
WHERE page.id = $id
UNION
SELECT 'product' AS sourcetable, products.id, products.image, product.other, product.stuff
But this is highly ugly. You're still forcing the DB server to do two queries in the background plus the extra work of combining them into a single result set, and then you have to do extra work to dis-entangle in your code to boot.
It's MUCH easier, conceptually and maintenance-wise, to do two seperate queries instead.

Categories