简体   繁体   中英

How do I select three tables from a blog including author, post and views and show in php mysql?

The table I am trying to connect for the third selection is the viewcount which is connected to the 'prints' table by prints.print_id = totalview.name

So the selection here works but adding the third table doesn't work. What is wrong with my query? WORKS!

$q = "SELECT artists.artist_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS artist, print_name, price, description, print_id, image_name FROM artists, prints WHERE artists.artist_id = prints.artist_id ORDER BY artists.last_name ASC, prints.print_id ASC";

ERROR!

$q = "SELECT artists.artist_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS artist, print_name, price, description, print_id, image_name, totalview.totalvisit AS totalvisit FROM artists, prints WHERE artists.artist_id = prints.artist_id, LEFT JOIN totalview ON totalview.print = prints.print_id ORDER BY artists.last_name ASC, prints.print_id ASC";

在此处输入图片说明 在此处输入图片说明

在此处输入图片说明

To show in a table as such:

$r = mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {

    // Display each record:
    echo "\t<tr>
        <td align=\"left\"><a href=\"browse_prints.php?aid={$row['artist_id']}\">{$row['artist']}</a></td>
        <td align=\"left\"><a href=\"view_print.php?pid={$row['print_id']}&name={$row['image_name']}\">{$row['print_name']}</a></td>
        <td align=\"left\">{$row['description']}</td>
        <td align=\"left\">{$row['description']}</td>
        <td align=\"right\">\${$row['price']}</td>
    </tr>\n";

The left join clause in the wrong position .. you can't add the left join clause after the where

and don't mixup esplicit and implicit join use ever explicit and last (hope) you have also a wrong comma after the where condition

$q = "SELECT 
        artists.artist_id
        , CONCAT_WS(' ', first_name, middle_name, last_name) AS artist
        , print_name
        , price
        , description
        , print_id
        , image_name
        , totalview.totalvisit AS totalvisit 
    FROM artists
    INNER JOIN  prints ON rtists.artist_id = prints.artist_id
    LEFT JOIN totalview ON totalview.print = prints.print_id 
    ORDER BY artists.last_name ASC, prints.print_id ASC";

Your SQL is structured incorrectly:

FROM artists, prints 
WHERE artists.artist_id = prints.artist_id, 
LEFT JOIN totalview ON

should be

FROM artists, prints 
LEFT JOIN totalview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM