简体   繁体   中英

How to perform SQL search on results of previous query

So in my phpmyadmin database, I have two tables, 'users' and 'palette'. In my php script, I am parsing data from 'users' and outputting the information on a seperate html file. My goal is to perform a SQL query on table 'palette' but to only search the results of 'users'. For example, my 'users' query returns 100 users. I want to now search those same 100 users on 'palette' to return their matching 'palette' information. Something like:

$pal_sql = "SELECT * FROM palette WHERE (user subscription_plan!='NULL')";
$pal_query = mysqli_query($dbs, $pal_sql);
$activerPalUsers = mysqli_num_rows("pal_query);
if(mysqli_num_rows($user_query) >= 1){
    while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
...

My desired result is something like the above code. Is this possible? What is the best approach. Below is my working code that searches for active 'users'.

The 'palette' table contains additional information about each user in 'users' which is sorted by id. I only want to parse palette data for active users. The first query shows active users but I cannot check if a user is active in the 'palette' table alone.

<?php
    $sql = "SELECT * FROM users WHERE subscription_plan!='NULL' ORDER BY id ASC";
    $user_query = mysqli_query($dbc, $sql);
    $activeUsers = mysqli_num_rows($user_query);
    if(mysqli_num_rows($user_query) >= 1){
        while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
            $userData .= "<tr>"; //general user information
            $userData .= "<td>";
            $userData .= $row["id"];
            $userData .= "</td>";
            $userData .= "<td>";
            $userData .= $row["email"];
            $userData .= "</td>";
            $userData .= "<td>";
            $userData .= $row["firstname"]."&nbsp;".$row["lastname"];
            $userData .= "</td>";
            $userData .= "<td>";
            $userData .= $row["subscription_plan"];
            $userData .= "</td>";
            $userData .= "<td>";
            $userData .= $row["ship_name"];
            $userData .= "</td>";
            $userData .= "<td>";
            $userData .=  $row["address"];
            $userData .= "</td>";
            $userData .= "</tr>";
        } 
    } else {
        $userData = "<tr><td colspan='6' style='text-align: center;'><strong>No listings found...</strong></td></tr>";
    }
?>

Let me know if my question makes sense or not, I can try to re-explain it in a different way if needed. Thanks for your time.

You can use JOIN feature in your query to join two tables' records on some conditions. So with just one query, you will be able to access to all the data you need. Also there will be less complexity in your php code.

You could create a view for the query using

CREATE VIEW AS

Now you can perform selects on that view similar to how you would a table.

http://www.w3schools.com/sql/sql_view.asp

It also sounds like you might want to use a JOIN to simiplify to a single query: http://www.w3schools.com/sql/sql_join.asp

"My goal is to perform a SQL query on table 'palette' but to only search the results of 'users'."

It's hard to figure out exactly what you want by above, but a qualified guess would be that you're looking how to join tables

Example:

In this case two tables are selected in the same query based on the relationship between values of users.id and palette.user_id)

SELECT * FROM palette 
JOIN users ON (users.id = palette.id)
WHERE (users.subscription_plan!=NULL)

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