-1

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.

1 Answers1

0

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.

Steve
  • 1,903
  • 5
  • 22
  • 30
  • Thank you, could I just create a dummy table instead of overriding the current tables? – Hannah Parks Feb 10 '18 at 02:40
  • What do you mean by overriding the current tables? You're just doing a simple SELECT with one JOIN. – Steve Feb 10 '18 at 02:48
  • I'm sorry, I thought this is what JOIN did. What exactly does JOIN do? Does it create a column then insert the values from the other table? – Hannah Parks Feb 10 '18 at 02:52
  • No, a JOIN simply links the two tables in the query, allowing you to pull data from both at the same time. It makes no changes to either table at all, creates no temp tables, etc. This is a good overview to get you started: https://www.w3schools.com/sql/sql_join.asp – Steve Feb 10 '18 at 02:55
  • Wow thank you so much! I finally get it! So, you just select what you want from the table then just join. And the `ON` part, is basically matching columns. – Hannah Parks Feb 10 '18 at 03:31
  • Glad I could help. And make sure you understand the difference between the types of joins. Knowing how and when to use LEFT and RIGHT will help you a lot one day. – Steve Feb 10 '18 at 03:46