Error trying to Select columns from different tables in MySql - php

I can't figure whether to use JOIN or UNION when trying to set this up, but when I found this answer: MySQL Select all columns from one table and some from another table I thought this might work. Turns out I still get an error. Is there something I am doing wrong with setup of this?
$sql = "SELECT user_images.*, users.profile";
Basically I have user images (user_images) in one table and I also want to display some user information such as the profile column in users table.

If you get an error please include it in your question. It's relevant information and you know we're going to ask what it was.
But this is a sample query for what you want to do.
SELECT user_images.*, users.profile FROM user_images JOIN users ON user_images.id = users.id;
Joins need a linking column to work correctly, something that is the same for both rows in both tables. I've used id as an example but it may not be correct for your specific situation.

Related

Mysql - How to get Newsfeed from users you follow

I've been working on this for some hours now and it's getting tiring. I want to get users posts from people I follow and myself, just like twitter does.
So there's a table users_posts that has all users' posts with column user_id to determine who made the post.
Another table users_followers that contains all followers ie. user_id follows following_id
So here's my query:
User ID = 2271
SELECT users_followers.following_id AS following_id, users_posts.id
FROM users_followers, users_posts
WHERE users_posts.user_id = users_followers.following_id
AND (users_followers.user_id =2271)
This query works but the problem is, it's kinda slow. Is there a faster way to do this?
Also, as you can see, I can only get posts from those I follow, and not myself.
Any help?
If I'm understanding your tables properly, I would do this with an explicit JOIN. I'm not sure how much that would speed things up versus the implicit JOIN you're using though.
SELECT following_id, p.id
FROM users_followers f
LEFT JOIN users_posts p
ON (p.user_id = f.following_id)
WHERE f.user_id = 2271
Chances are, adding an index or two to your tables would help.
Using the MySQL EXPLAIN command (just put it in front of your SELECT query) will show the indexes, temporary tables, and other resources that are used for a query. It should give you an idea when one query is faster or more efficient than another.
Your query is fine as written, provided you have properly indexed your tables. At a minimum you need an index on users_posts.user_id and users_followers.following_id.
A query can also be slowed by large numbers of records, even when it is fully indexed. In that case, I find phpmyadmin to be an invaluable tool. From the server page (maybe localhost) select the Status tab to see a wealth of information about how your mysql server is performing and suggestions for how to improve it.

Mysql INNER JOIN and Resource #9

I am working on a UCP(User control panel) where a user loggs in by an unique ID that is given at registration.
My problem is i have two tables in my database:
serverplayers - Holds user ID and name and all of his info
and
vehicles - holds vehicle owner and vehicle model.
So what I need to do is connect those two together by users "Username".I tried Mysql INNER JOIN like this.
$car = mysql_query('SELECT vehicles.vOwner, vehicles.vModel
FROM vehicles
INNER JOIN serverplayers ON vehicles.vOwner = serverplayers.User
WHERE vehicles.vOwner = '.$ro['User'].'');
After this I get Recourse #9, i read that it's not an error it's just that the result is empty?I may have error's or problems in my mysql query so please tell everything whats wrong.
But what's the problem I think is that a vehicle is only connected by it's owner's name and doesn't contain UserID.So what I need is basically show a User what car he has by Connecting his username with vOwner.I'm not a very bright boy when it comes to mysql or php I'm just learning and I came across this thing as mysql INNER JOINS that I think is very usefull(but the main reason is that I don't want to recode the whole server and the UCP).
Vehicles:
ServerPlayers:
ServerPlayers table http://img94.imageshack.us/img94/3369/qebq.png
I'm not sure what $ro['User'] is, but it sounds like an ID based on your question. In that case I would just switch vehicles.vOwner to serverplayers.UserID in that comparison. I would also highly recommend changing your schema so vOwner is the ID and not the username (what if the username changes in one of the tables?) It also doesn't look like you need to join on serverplayers for anything.
The "resource" is a mysql results resource. You can't get anything out of it just by looking at it -- you need to run fetch operations on it:
$row = mysql_fetch_assoc($car);
I recommend using PDO or mysqli over the deprecated ext/mysql.

Avoid repeating records displaying in MySQL / PHP

This is a very basic PHP MySQL question that I need answered.
When displaying data from a table, I need to avoid/prevent duplicates. I am trying to populate a drop down menu with a list of countries from the database. So that... If there are many records with the Country "US" ... it will only display ONCE in the drop down list. I'm not sure if this should be MySQL coding or PHP coding. ??
DB INFO:
table: dealers
field: Country
If possible, please post the exact php and/or MySQL coding that can be COPY/PASTED directly into my code.
Thanks.
The SQL that you want is:
SELECT DISTINCT Country
FROM dealers
Use the distinct keyword to get unique records.
SELECT DISTINCT `field1`FROM `table1`
I recently came across the same issue in my application. I had joined two tables from my database and for the life of me couldn't figure out how to get rid of the duplicate data that was coming from the table I joined. After doing a little research I found that using DISTINCT would only give me the values that were different. This solved my problem and should solve yours as well.
select DISTINCT document_type,document_subtype,document from document_details where employee_id='ERP170102547549543' ORDER BY document_type ASC

Selecting just one row from a joined MySQL query

I am trying to make a social networking site in PHP/MySQL. I am currently developing status update and comments on status's system. I am trying to show all status of mine and comments on certain status. For doing that I have two tables: comment and user_status.
I have used this MySQL query,
SELECT * FROM user_status LEFT JOIN
comment ON id_status = comment.status_id
WHERE sender_id = '$id2'
OR receive_id = '$id2'
/* $id2 is my id */
I have successfully showed status and one comment. But the problem is, when the number of comments are more than one, then the status shows more than one times. How much same status will be showed depends on how much comments available on certain status. But I would like to be able to display same status only one time, and display more than one comments (if available) on certain status.
This isn't so much a PHP problem as it is confusion about how SQL joins work.
It sounds as if what you really want is not so much a join but a distinct set of records from two tables. Until your SQL skils develop a little more, consider simplifying things by making two queries -- one each for comment and user_status. Also consider requesting just the specific fields you're interested rather than using SELECT *.
Here is a visual explanation of different SQL joins, in case you want to pursue this with a single query.
I assume that you are not displaying the raw results from the query, but rather are piping them to an html page to display. Display only the most recent status in a textbox. then display thin a table or list an ordered list of the comments.
Your query is correct.

using joins or multiple queries in php/mysql

Here i need help with joins.
I have two tables say articles and users.
while displaying articles i need to display also the user info like username, etc.
So will it be better if i just use joins to join the articles and user tables to fetch the user info while displaying articles like below.
SELECT a.*,u.username,u.id FROM articles a JOIN users u ON u.id=a.user_id
OR can this one in php.
First i get the articles with below sql
SELECT * FROM articles
Then after i get the articles array i loop though it and get the user info inside each loop like below
SELECT username, id FROM users WHERE id='".$articles->user_id."';
Which is better can i have explanation on why too.
Thank you for any reply or views
There is a third option. You could first get the articles:
SELECT * FROM articles
Then get all the relevant user names in one go:
SELECT id, username FROM users WHERE id IN (3, 7, 19, 34, ...)
This way you only have to hit the database twice instead of many times, but you don't get duplicated data. Having said that, it seems that you don't have that much duplicated data in your queries anyway so the first query would work fine too in this specific case.
I'd probably choose your first option in this specific case because of its simplicity, but if you need more information for each user then go with the third option. I'd probably not choose your second option as it is neither the fastest nor the simplest.
It depends how much data the queries are returning - if you'll be getting a lot of duplicate data (i.e. one user has written many articles) you are better off doing the queries separately.
If you don't have a lot of duplicated data, joins are always preferable as you only have to make one visit to the database server.
The first approach is better if applicable/possible:
SELECT a.*, u.username, u.id
FROM articles a
JOIN users u ON u.id = a.user_id
You have to write less code
There is no need to run multiple queries
Using joins is ideal when possible
Get the articles with one query, then get each username once and not every time you display it (cache them in an array or whatever).

Categories