0

I need to join tables but couldn't really understand what is on the websites. Could you help me?

I have the tables

Products

ID / product_name / product_description / product_code /

Users

ID / username / username_id / password /

Checkedout

ID /product_name /product_des /username /username_id /checkedout_date / return_date /

How can I join from Products (product_name / product_description ) and from Users (username / username_id) to Checkedout?

Thanks very much.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Allen
  • 3
  • 2
  • 1
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Ahmet Emre Kilinc Nov 23 '17 at 18:28
  • https://stackoverflow.com/questions/10269809/mysql-join-query – Igor Monteiro Nov 23 '17 at 19:22

2 Answers2

0

Joining tables is fairly simple. I'll leave it to you to continue your learning, but here is how you join those tables, so that you have a reference to better understand what you're learning.

The idea is that you just have to list your tables (in this case, I've added arbitrary aliases to each table - c, p, and u), and specify whether you want an inner or outer join, and then specify which columns to join on. You join on columns that have the same value in both tables. For example, Checkedout has a username_id and the Users table has an ID. You can use all the same logic you would use in a WHERE clause when specifying what to join on (including AND and OR)

select
    *
from
    Checkedout c
    inner join Products p
        on (c.product_name = p.product_name and c.product_des = p.product_description)
    inner join Users u
        on (c.username_id = u.ID)
RToyo
  • 2,877
  • 1
  • 15
  • 22
0

First you need to update your database schema so that the tables are normalized. Suggested schema for Checkedout:

Checkedout - id, product_id, user_id, checkedout_date, return_date

Now you can write a join query very easily(something like this):

select u.username, p.product_name, co.checkedout_date from Checkedout as co join Users u on u.id = co.user_id join Products p on p.id = co.product_id where u.id = 1;

Aman
  • 372
  • 4
  • 13