PHP mysql how to join two tables to link name from one table and post from other table

Ghost

So I am very bad at this anyway right now I have this code

$test = "SELECT `status`,`pubdate` FROM `status` ORDER BY `pubdate` DESC";
$stmt = $db->prepare($test);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    echo "<section class='statusar'>"."<article>";
    echo $row['status'];
    echo $row['pubdate'];
    echo "</article></section>";
}

and it shows the post but now I want to take users first name from another table and display it with the post. How do I do it?

The two tables I have look like this

This is the table with the post

Status_id  int          unsigned       auto_increment  primary
user_id    int          unsigned index
status     varchar(600)
pubdate    datetime

This is the other table

user_id   int         unsigned auto_increment primary index
firstname varchar(30)
surname   varchar(30)
username  varchar(30) unique
password  varchar(90)

I have done so they have a relation between the user_ids

EDIT

I think I figured it out because now it works :D So now when I use

SELECT status, pubdate,firstname,surname FROM status INNER JOIN user ON status.user_id = user.user_id ORDER BY status.pubdate DESC

It displays all the things I want and I also tried

SELECT * FROM status INNER JOIN user ON status.user_id = user.user_id ORDER BY status.pubdate DESC";

Which also works thanks for the help guys :D

sgeddes

Just use an inner join:

SELECT s.status, s.pubdate, u.firstname
FROM status s
    INNER JOIN usertable u ON s.user_id = u.user_id
ORDER BY s.pubdate DESC

This assumes the user_id exists in the usertable. If that might not be the case, you'd want to use an outer join instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to join two tables with multiple IDs from one table used in other table?

From Dev

Joining two tables in PHP/MySQL and displaying elements from both when data from one table is null and the other table's data is not

From Dev

Is possible to join two tables (One Mysql, the other Oracle) from two different servers in PHP?

From Dev

JOIN tables to get name of ID from other table

From Dev

MYSQL query SELECT from one table and join results with other table

From Dev

Delete from one table using join on two tables

From Dev

Join two tables and return only one row from second table

From Dev

MYSQL: How to join two tables using Inner join and then calculatin the total number from the second table for the following examples

From Dev

Update one table based on data from two other tables

From Dev

Select values from one table based on two other tables (relational)

From Dev

Update one table based on data from two other tables

From Dev

Selecting rows absent from one table against two other tables?

From Dev

MYSQL: Query two tables and join results from second table to an array

From Dev

MYSQL JOIN two tables limit results from second table by date

From Dev

MySql multiple select from two tables and join their results to a third table

From Dev

MYSQL: Query two tables and join results from second table to an array

From Dev

MYSQL join one colum from one table to two count() in another

From Dev

MySQL Combine data from two tables with table name

From Dev

MySQL Combine data from two tables with table name

From Dev

MySQL, Join two rows from one table and ignore doublons

From Dev

running a query selecting data from one table and one of two possible other tables depending on data in the first table

From Dev

MySQL - how to write stored procedure to insert data in to table from two other tables

From Dev

Join column name with value from other table

From Dev

Update a third table from two other tables

From Dev

How to get value from one table column when two columns of the same name exist in an sql join

From Dev

SQL join - Looking to search one table for keyword, two other tables

From Dev

Compare Two Tables, Get Value From Table and Multiply It - PHP MySQL

From Dev

PHP + MySQL - one table holding reference to IDs from multiple tables

From Dev

How to get an average from a table that was originated by other 2 tables in MySQL?

Related Related

  1. 1

    How to join two tables with multiple IDs from one table used in other table?

  2. 2

    Joining two tables in PHP/MySQL and displaying elements from both when data from one table is null and the other table's data is not

  3. 3

    Is possible to join two tables (One Mysql, the other Oracle) from two different servers in PHP?

  4. 4

    JOIN tables to get name of ID from other table

  5. 5

    MYSQL query SELECT from one table and join results with other table

  6. 6

    Delete from one table using join on two tables

  7. 7

    Join two tables and return only one row from second table

  8. 8

    MYSQL: How to join two tables using Inner join and then calculatin the total number from the second table for the following examples

  9. 9

    Update one table based on data from two other tables

  10. 10

    Select values from one table based on two other tables (relational)

  11. 11

    Update one table based on data from two other tables

  12. 12

    Selecting rows absent from one table against two other tables?

  13. 13

    MYSQL: Query two tables and join results from second table to an array

  14. 14

    MYSQL JOIN two tables limit results from second table by date

  15. 15

    MySql multiple select from two tables and join their results to a third table

  16. 16

    MYSQL: Query two tables and join results from second table to an array

  17. 17

    MYSQL join one colum from one table to two count() in another

  18. 18

    MySQL Combine data from two tables with table name

  19. 19

    MySQL Combine data from two tables with table name

  20. 20

    MySQL, Join two rows from one table and ignore doublons

  21. 21

    running a query selecting data from one table and one of two possible other tables depending on data in the first table

  22. 22

    MySQL - how to write stored procedure to insert data in to table from two other tables

  23. 23

    Join column name with value from other table

  24. 24

    Update a third table from two other tables

  25. 25

    How to get value from one table column when two columns of the same name exist in an sql join

  26. 26

    SQL join - Looking to search one table for keyword, two other tables

  27. 27

    Compare Two Tables, Get Value From Table and Multiply It - PHP MySQL

  28. 28

    PHP + MySQL - one table holding reference to IDs from multiple tables

  29. 29

    How to get an average from a table that was originated by other 2 tables in MySQL?

HotTag

Archive