简体   繁体   中英

Select data from 3 different mysql tables

I have 3 tables and im trying to achieve a final table by putting some specific data from each table. the tables are as follow:

First table called tbl_category and it is as follow:

+------+---------------+----------------+
| cid  | category_name | category_image |
+------+---------------+----------------+
| 1    | cars          |  1.jpg         |
+------+---------------+----------------+

Second table named as tbl_wallpaper is like this:

+------+---------------+----------------+-----------+
| id   | cat_id        | scat_name      | image     |
+------+---------------+----------------+-----------+
| 33   | 1             |  blue cars     | 001.png   |
+------+---------------+----------------+-----------+

Third table called tbl_scategory and it is like this:

+------+---------------+
| scid |scategory_name |
+------+---------------+
| 1    | blue cars     |
+------+---------------+

the objective im trying to reach is to get something like this

"HD_WALLPAPER": [
    {
        "cid": "1",
        "scat_name": "blue cars",
        "category_name": "cars",
        "category_image": "1.jpg",
        "category_image_thumb": "001.png",
        "total_wallpaper": "1"
    },

Means that the data comes from the table tbl_category and tbl_wallpaper and tbl_scategory . im trying to get the total of wallpapers that are stored in the table tbl_scategory (sub category of tbl_category)

im using this code but it brings the whole wallpapers column

if(isset($_GET['cat_list']))
{
    $jsonObj= array();
    $cat_order=API_CAT_ORDER_BY;
    $query="SELECT tbl_category.cid, tbl_category.category_name, tbl_category.category_image, tbl_scategory.scid, tbl_wallpaper.scat_name FROM tbl_category ,
    tbl_scategory, tbl_wallpaper
    ORDER BY tbl_category.".$cat_order."";
    $sql = mysqli_query($mysqli,$query)or die(mysql_error());


    while($data = mysqli_fetch_assoc($sql))
    {
        //Wallpaper count
        $query_wall = "SELECT COUNT(*) as num FROM tbl_wallpaper WHERE scat_name='".$data['cid']."'";
        $total_wall = mysqli_fetch_array(mysqli_query($mysqli,$query_wall));
        $total_wall = $total_wall['num'];   
        $row['cid'] = $data['cid'];
        $row['scat_name'] = $data['scat_name'];
        $row['category_name'] = $data['category_name'];
        $row['category_image'] = $file_path.'images/'.$data['category_image'];
        $row['category_image_thumb'] = $file_path.'images/thumbs/'.$data['category_image'];

        $row['total_wallpaper'] = $total_wall;


        array_push($jsonObj,$row);

    }

    $set['HD_WALLPAPER'] = $jsonObj;

    header( 'Content-Type: application/json; charset=utf-8' );
    echo $val= str_replace('\\/', '/', json_encode($set,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    die();
}

i dont know what is the problem

NB: the tables are more rows i just put those parts to show the example

Try this:

SELECT t1.cid, t1.category_name, t1.category_image, t2.scat_name, t2.image
FROM tbl_category AS t1, tbl_wallpaper AS t2, tbl_scategory AS t3
WHERE t1.id = t2.cat_id
AND t2.scat_name = t3.scategory_name

I hope it would solve Your problem

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