Unique Columns in SQL Views - php

In my database I have one table that contains a complete list of products, and another table that contains the same list of products on the x-axis, with a list of customers on the y-axis, where the value for each product can be 1 or 0 depending on whether that customer can view that product. My SQL looks like this:
SELECT products.product_code, products.product_type, products.product_category, products.product_title, products.product_description
FROM product_lists
INNER JOIN products
ON product_lists.product_code=products.product_code
WHERE product_lists.customer="1"
ORDER BY products.product_code
My problem is that I would like to create a view of this result for each customer to use as that customers product table, however when I create it I get the message "This table does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available." even though the product_code field is set as a primary key in both the products table and the product_lists table.
How can I create a join/view that uses the primary key from the table(s) it was created from? In short I would like the product_code field to become the primary key of my view.
Thanks!

I think the problem is the join. You can fix this by moving the condition to the where clause. MySQL doesn't allow subqueries in the from, but it does in the where:
SELECT p.product_code, p.product_type, p.product_category, p.product_title, p.product_description
FROM products p
WHERE EXISTS (SELECT 1
FROM product_lists pl
WHERE pl.product_code = p.product_code AND
pl.customer = 1
)
ORDER BY p.product_code;

Related

How do i display the information of foreign key instead of id in MySQL and PHP?

just like the caption, how do i fetch information from foreign key instead of id?
Here are the first table:
and here are the second table:
i want to display product_name and unit_price instead of id, how to do it?
You need to use join
Your query should look something like this:
SELECT ft.id, pr.product_name, pr.unit_price from firsttable as ft join product as pr on ft.product_id = pr.product_id;
Where firsttable is the table of the first screenshot and product is the table from the second screenshot.
I don't know what you need from the first table so I just took a column id, however I do not know if you have this column. Here you just put whatever you need from the first table.

Copy rows in MySQL table multiple times and adding additional column

I have created MySQL table in database. Table name is products and the columns are( prodict_id(pk) product_name and pack_size) as shown in the figure below.
What I want to do is , copy all the rows in the table and add additional information in additional column called (buyer_name) so each product is associated with a specific buyer which makes it unique
Is there a way I can achieve this using query? Where I can give a list of buyers and it attaches it to all rows in table?
p.s I have almost 700 rows in my table and I have 12 buyers, so if I do it manually, it will consume too much time
As per your comment your buyer details are in a table and you want to map each product with each of the buyer then you can write your insert query like below:
insert into newtable
select t1.*, t2.buyername from products t1 join buyers t2
DEMO
You can use where clause also to filter some results from either of the table.
It seems you want to automate the data insertion from product table to buyer table. How about, if you select that fetches all the buyers first and then you insert into buyer table.
It can be based on subquery where insert is the outer one and select is the nested one.
Good luck !

Update table without primary key

Im building a shop and each product has a unique set of attributes.
the product db looks like this:
products_id, relation_id, product_name, description,
price, glass, shipping, img, cat, subcat, model
Since every product has several (~40) different attributes unique to that product only ive created a second table to store them.
products_id, att_name, att_val, att_head, att_standard, att_order
This works fine, because there will never be two unique rows.
The problem, however, is when i need to modify the attributes content.
using MySQL Workbench i can modify a row using something like
UPDATE product_attributes SET att_val='1500' WHERE products_id='112' AND att_head='threshold'
This however, doesn't seem to work when i update from my php script.
Is there an easy way to modify the table to support updating?
Im well aware of the stupidity not having an unique column.
But im not sure how to make the two tables relate.
Where should i store the unique id of the attributes?
One choice,
add a primary key "auto_incremented" into the product_attributes table...
ALTER TABLE `product_attributes` ADD `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
This Id is just for CRUD (Create Read Update Delete) task.
The only relation you can have between your two tables is the products_id wich allow you to have few product_attributes for one product
Since 1 product has more than 1 unique attributes that u store in a second table, you should use the ID of the table product and store it in the second table with the attributes.
Hope this is what u need?

MySQL Merge 3 Tables into 1

I am creating a search box in PHP and using MySQL as the database but when searching there are 3 tables, Colours, Products and Categories, these all have an ID number and can be linked. I have tried to use INNER JOIN, LEFT, RIGHT, everywhere but no luck, the query will sometimes work, spit out multiple items. So I am looking at creating a one-table-fits-all scenario where all the table field names will be in one and I can easily query that table. I have manually created the table but is there anyway of coping the data from the 3 tables into that main one? I do not mind doing it separately if it is a query that only handles one table but I would love not to have to manually type all the data as there is 600+ rows.
Here is the code I am currently trying to use:
SELECT
categories.Product_Type, items_colors.ColourImageurl,
items_list.description, items_list.Description2,
items_list.title, items_list.id, categories.title AS title2,
items_colors.itemID, Colour Name
FROM items_list
LEFT JOIN categories ON categories.Product_Type = items_list.CatID
LEFT JOIN items_colors ON items_list.id = items_colors.itemID
WHERE items_list.visible = 1 AND
Colour Name LIKE '%".$search."%'
Categories defines what type of product you are selecting, items_list has a list of all the sub category names and item_colors has a list of all the colour names that link to the items_list products. When I use this query it outputs 4 copies of one item and I'm not sure why.
If you are getting data from a query, you can use "create table as select" statement to create the new table, with data from old tables.
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
select_statement
check here for more info : http://dev.mysql.com/doc/refman/5.1/en/create-table.html

A mysql query and a php script for a simple digital sticker album

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'
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.
Correct me if I've misunderstood, and I will update accordingly.
You are outputting a table of all your items.
Each item listed
will have an empty "box", that is filled if the current user has this
item.
To do this, you should only need 2 queries-- one to fetch all your items, another to fetch the item IDs of the current user's inventory.
// Get all current user's items
$qUsersItems = "SELECT items.id AS itemID FROM items, inventory WHERE items.id = inventory.itemid AND inventory.userid = " . $givenUserID .
$rUsersItems = mysql_query($qUserItems);
// Store the user's items' IDs into an array
while (list($itemID) = mysql_fetch_assoc($rUsersItems))
$usersItems[] = $itemID;
// Get all items
$qAllItems = "SELECT id, name FROM items";
$rAllItems = mysql_query($qAllitems);
// ... (To wherever you are outputting your items list)
while ($item = mysql_fetch_assoc($rAllItems)) {
if (in_array($item['id'], $usersItems)) // Since this ID is in the users list of items (ids)
$fillBox = "HTML to fill box";
else
$fillBox = "";
/* Your "per item" HTML here, for example (assuming you have outputted table tags before and after the while loop */
echo<<<HTML
<tr>
<td>{$item['name']</td>
<td>{$fillBox}</td> <!-- This would be your empty box /-->
</tr>
HTML;
Note: This code is untested, and unfinished for obvious reasons. After I wrote this, I realize why people ask to show what you have done first.

Categories