MySQL Merge 3 Tables into 1 - php

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

Related

SELECT FROM 100 tables in 1 database

I am trying to SELECT id, description, title FROM table1, table2, table100
Say I get this working, is it better for me to just combine all my tables in phpmyadmin?
The problem is I have around 100 tables all of different categories of books so I want to keep them seperated in their individual tables.
I am trying to make a search engine that searches all the books in the entire database. All tables have the same column names.
So really all I really am trying to do is search the entire database's tables for an id, description, title. My search works, just I can only search 1 table and every solution online I have found only really works efficiantly with 2 or 3 tables.
Thanks in advance.
The best is to redesign your database, everything into a single table with an additional "category" column.
in the meantime, you can create a view which union the tables with an additional column for the category.
I recommend redesign the model and unifique this 100 tables to 1, and add a new column with category but integer value, not string value. In this way, you can index the category column with the other fields (id, description, title) for speed up the query.
This resolution is more easy for avoid pain later.
I recommend keeping one table A with id, description, title, category and create another table B with categories. Table A has to have a foreign key with table categories. Then create a query to retrieve the books with a specific category.
Example:
SELECT id, description, title, category FROM books WHERE category = "drama"
I think it speaks to the database design itself as mentioned by most here. You've a few options depending on how much time you have on your hands:
(Short Term / Quick Fix) Central table with all your current fields plus category as a flag to differentiate between the current tables you have. So your insert will be something like "INSERT INTO newtable (ID,AssetID,ServiceID,Category) SELECT id, description, title, 'Fiction' FROM table1 ;"
If you tables are incrementally named like table1, table2 upto table100, you could then maybe write a quick php script that will iterate through the insert loop while incrementing on table on each iteration until the last table.
In the long run, you could invest in a json field that will house all your other data excluding keys that pertaining to a single entry

sql query from many table in PHP

I have an mp3 downloading site and I want to create a search page, but I am in simple confusion. I have two tables, one that stores movie details like movie name and movie cast, and another one where all the songs of movies are stored. The structure of two tables is shown below:
Table 1: name categories
column :5
id,cat,cast,year,type
here id is primary key
cat is album/movie name
cast is all actores details of movie
year is releasing year and type is movie type
Table 2: files
column :4
id,cat,preview,file
Here my user can search with any keywords, and I can extract rows by sql query but here are two tables so I am confused what to do?
The user can input previews from table and can enter cat, type, cast, type and year from table 1.
Your table structure is not clear, But using join in query is not that complicated as you think, try and learn. Good luck
SELECT Category.cat, Category.cast, Category.year, Category.Type, File.preview, File.file
FROM category As Category
LEFT JOIN files As File On Category.cat = File.cat
WHERE Category.cat LIKE '%search%' OR Category.cast LIKE '%search%'
OR File.preview LIKE '%search%'
etc

Unique Columns in SQL Views

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;

Table Name stored in column used as part of query?

I cant seem to find information related to this question.
I have four tables: pages, items, linktable, datatable
The pages table has a ID that links to the Items table.
The items table has an ID and Table Name field and ID that reference the table which has the actual data for the item.
aka:
Pages_Table:
pg_id
pg_title
Items_Table:
pg_id
item_type
item_id
link_table:
item_id
data_id
data_table_name
I need select data based on the data_table_name which varies, there are 10 different data_tables
I cant seem to find anything related to incorporating this into a SINGLE Query.
With multiple queries obviously not an issue. Essentially its selecting a database table dynamically to be included in the query.

Display data category wise

We have six categories Example :- a,b,c,d,e,f.
In each category we have products.
In a category i have 2 products & in b category i have 1 product.
I'm fetching this data from database.
product_id category_id
1 1
2 1
3 2
What should be best logic to display records, so two categories not display next to it.
Output product id like :- 1,3,2
I do not think such function exists. Even if you use DISTINCT it will only select the ones that do not duplicate and leave the out the rest, which is not what you need. So, I think I would run two queries at the same time, the first one will choose all the distinct queries, like 1,2,3,4 and after that, you can create a second query just to query normally all products. This way, let say.. if you had products by type like 1,1,2,3,3,3,4,4,4,4,5,5,5, then, the first sql query would list 1,2,3,4,5 and the second will just query normally, but still, your clients will see the distinct ones first.
SELECT DISTINCT type FROM table ORDER by id

Categories