in Education by
I'm trying to produce a page that would function a bit like a digital sticker album. My SQL knowledge isn't very good so I need a bit of help finishing off what I have so far. I want to display a list of all available stickers but then also show whether or not a user has a sticker. So the idea is to display a list of empty boxes(items) and then display an image inside the box if the user owns the sticker. The two relevant tables I have are called "items" and "inventory". Items contains all the available stickers and inventory contains the stickers owned by the users. Here are the available columns: CREATE TABLE items ( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) ) Engine=InnoDB; CREATE TABLE inventory ( userid INT UNSIGNED, itemid INT UNSIGNED, -- FOREIGN KEY (userid) REFERENCES users (id), FOREIGN KEY (itemid) REFERENCES items (id), UNIQUE (itemid, userid) ) Engine=InnoDB; I'm open to suggestions on the best way to go about doing this using PHP and MySQL, but I think what I need is a query to return a list of all the item names and then another column to flag whether the user has the item. I can then loop through the items in php and then use a conditional based on the second column to show if the sticker is there or not. So far i've got as far as the below query but it needs to only show items for the current user. Sticking in a 'where' clause doesn't work either as it then only shows the inventory items and not the NULL items (that is, it doesn't include all items). SELECT items.name, inventory.userid FROM items LEFT JOIN inventory ON items.id = inventory.itemid SELECT items.name, inventory.userid FROM items LEFT JOIN inventory ON items.id = inventory.itemid WHERE inventory.userid = '$userid' JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Try moving the userid test to the JOIN condition: SELECT items.name, inventory.userid FROM items LEFT JOIN inventory ON items.id = inventory.itemid AND inventory.userid=? That way, when the join condition fails (such as when the item is in another user's inventory), the item itself is still included in the result, but with a null userid. It can also make use of an appropriately defined index on table inventory.

Related questions

0 votes
    I have a script which loops over tables and fields doing a find and replace. The script takes around ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    I have one script and many websites, because they are on the same server how can I execute same ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have one script and many websites, because they are on the same server how can I execute same ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I am struggling to figure out how to specify a MOTO payment with Stripe API and PHP. I believe I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    In ASPNET, I grew to love the Application and Cache stores. They're awesome. For the uninitiated, you ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    In ASPNET, I grew to love the Application and Cache stores. They're awesome. For the uninitiated, you ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    In ASPNET, I grew to love the Application and Cache stores. They're awesome. For the uninitiated, you ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    In ASPNET, I grew to love the Application and Cache stores. They're awesome. For the uninitiated, you ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I am aware that I can use array_unique(array_merge($a,$b)); to merge two arrays and then remove ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I want to load Google fonts in the head-section of my page using a php-function. My -area looks like this: ... ... Now ... I do this, there will be a "1" displayed (because of the...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I want to load Google fonts in the head-section of my page using a php-function. My -area looks like this: ... ... Now ... I do this, there will be a "1" displayed (because of the...
asked May 30, 2022 in Education by JackTerrance
0 votes
    I want to load Google fonts in the head-section of my page using a php-function. My -area looks like this: ... ... Now ... I do this, there will be a "1" displayed (because of the...
asked May 26, 2022 in Education by JackTerrance
0 votes
    Is it possible to cache database connections when using PHP like you would in a J2EE container? If so, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I want to load Google fonts in the head-section of my page using a php-function. My -area looks like this: ... ... Now ... I do this, there will be a "1" displayed (because of the...
asked May 17, 2022 in Education by JackTerrance
0 votes
    How can I get the value of the amp selector and save it in a php variable 1 1 1 I tried ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
...