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.
Related
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
I have a forum that is saved to a mysql database the table for the actual posts is call _discuss_posts
In here I have the ID of the post and another column call parent_id (there are loads more but those are the two columns that are of interest to me.)
So basically all replies and new posts are added to this table if it is a reply it has a parent_id that corresponds to the first post ID.
Now, this is where I'm getting confused. I'm trying to count the number of replies each ID has, I could do:
SELECT count(*) FROM `qwt1v_discuss_posts` WHERE `parent_id`=4968
But I don't want to manually change the parent_id each time, there is over 10k post.
So my question is this, I'm trying to figure out if there is a query I can do to automatically count based off of the ID. So for example, the first row ID is 2000 and I want to check all the other rows in the column parent_id to find the ones that are allocated to the ID 2000 once I get to the end of the table, move to the next row to grab the next ID value lets say ID value 2001 and do a search for the that in the column parent_id.
Hopefully i made some sence as its a all jumbled up in my own head so apologies if i havn't been very clear.
Right now i'm trying to simply find an SQL query but eventualy i will be moving to a php script so that i can show this in a table for some dashboards.
Thanks to whom ever reads this
Best
if you need the count of each parent_id you should use group by eg:
SELECT `parent_id`, count(*)
FROM `qwt1v_dis, uss_posts`
group by `parent_id`
I have 4 tables, and I want to fetch data from all the tables, I can do this by fetching data one by one from each table but I want to do it by using JOIN.
Main Table (which contains ids of other table data)
2, 3, 4 tables
now I want to fetch these fields.
from Table 1 (Main Table) - franchise_name, franchise_phone
from Table 2 (State Table) - state_name
from Table 3 (City Table) - city_name
from Table 4 (Area Table) - area_name
the first table contains ids of everything which I need to fetch from other tables.
but area_id in the main table is inserted as a string in the same row separated by (,) in field franchise_area.
I tried using FIND_IN_SET but did not work.
Read all 1-1 referred data using Join, cycle through data exploding area_id column
area_id -------> ($value = explode($row->area_id, ',')
then read data from database and insert into response array (or object).
Of course all of this operation must be done into the model...
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
Ok, I have 3 tables;
'bu_blogs' contains blogs which have a unique blog_id.
'bu_sites' contains sites which have a unique site_id.
'bu_blogs_done' contains id, blog_id and site_id. A new row is added to this table every time a site_id is submitted to a blog_id.
What I want to do is SELECT 2 random rows from 'bu_blogs' where a field in 'bu_blogs_done' for the particular blog_id and site_id does not exist, i.e it haven't been submitted to that blog_id yet.
Thanks
Stian
If your table isn't too big (e.g. approx 100 rows), you can use something like this simple example for the random part:
SELECT * FROM bu_blogs ORDER BY RAND() LIMIT 2
Then it's just a case of adding a WHERE clause to filter out the ones that exist in bu_blogs_done.