Is it possible to break a large MySQL table into smaller related tables?
for instance imagine the table:
x_data--
id | offer_price | offer_text | free_gift | free_gift_category | model_name | model_description | tariff_name | tariff_rental | tariff_minutes | tariff_texts | tariff_data | retailer_name | retailer_description
is it possible to split this out into separate tables and build relationships so the resulting tables look like:
offers--
id | offer_price | offer_text | free_gift_id | model_id | tariff_id | retailer_id
free_gifts--
id | free_gift | free_gift_category_id
free_gift_categories--
id | free_gift_category
models--
id | name | description
tariffs--
id | name | rental | minutes | texts | data
retailers--
id | name | description
i.e can I do an insert on a sub select or join?
something like:
$query = "INSERT INTO retailers (name,description)
(SELECT retailer_name, retailer_description FROM x_data) as retailer,
(SELECT MAX(id) as retailer_id FROM retailers) as retailer_id,
INSERT INTO tariffs (name, rental, minutes, texts, data) as tariffs, ...."
if this is possible how would I go about it?
Related
I have two different tables with different column name
Table 1 users
+-----+----------+---------+---------+------------+
| id | name | username| image | email |
+-----+----------+---------+---------+------------+
| 1 | John1 | exmple1 | img1 |a#gmail.com |
| 2 | John2 | exmple2 | img2 |b#gmail.com |
| 3 | John3 | exmple3 | img3 |c#gmail.com |
| 4 | John4 | exmple4 | img4 |d#gmail.com |
+-----+----------+---------+---------+------------+
Table 2nd Company
+-----+----------+------------+---------=------+-----------+
| id |company_name | username| description | founded |
+-----+-------------+---------+----------------+-----------|
| 1 | john1 | exmple1 |description1 | 2016 |
| 2 |CompanyName2 | exmple2 |description2 | 2016 |
| 3 |CompanyName3 | exmple3 |description3 | 2016 |
| 4 |CompanyName4 | exmple4 |description4 | 2016 |
+-----+-------------+---------+---------=------+-----------+
Now whenever a user type any input in search bar an ajax request is made to php file which checks for if that name of user exist in database or not
so for example if user type input john then a query should run which will check john in users table and company table & if there is a user name john & if there is a company named john it should fetch both the result.
how can i achieve this, i tried using UNION in my query but it said columns are different
Currently this is my query
$go = mysqli_query($connecDB, "SELECT name, img,username FROM users WHERE name LIKE '$q%' LIMIT 0,10");
Now people might be thinking what i really want!
i want a single query that will check for input in both table & fetch their details
You can use union
select id, name ,username, image, email, null, null
from users
where name LIKE concat('$q', '%')
union all
select id, company_name as name ,username, null, null, description, founded
from Company
where name LIKE concat('$q, '%')
order by name limit 10
i have one table products and one table locations. products table have two columns pickLocation and recLocation. in locations table i have id and name columns. pickLocation and recLocation have id from location table. how can i join table in codeigniter.
Here is my code
$this->db->select("locations.name as plname");
$this->db->select("locations.name as rcname");
$this->db->join("locations","locations.id=products.pickLocation","LEFT");
$this->db->join("locations","locations.id=products.recLocation","LEFT");
Here is Products table
+----+--------------+-------------+
| Id | pickLocation | recLocation |
+----+--------------+-------------+
| 1 | 12 | 23 |
| 2 | 12 | 12 |
+----+--------------+-------------+
Here is Location table
+----+-----------+--+
| Id | name | |
+----+-----------+--+
| 12 | Location1 | |
| 23 | Location2 | |
+----+-----------+--+
I want result like this
+-----------------------+
| 1 Location1 Location2 |
+-----------------------+
| 2 Location1 Location1 |
+-----------------------+
Use aliases. Also, your product table never appears except in the join clause. It should also be in the from.
$query = $this->db->select("p.id, l1.name as plname, l2.name as rcname")
->join("location l1", "l1.id = p.pickLocation", "left")
->join("location l2", "l2.id = p.recLocation", "left")
->get("product p");
You need to use aliases, to be able to distinguish between these two joins. Try something like this:
$this->db->select("pickLoc.name as plname");
$this->db->select("recLoc.name as rcname");
$this->db->join("locations as pickLoc","pickLoc.id=products.pickLocation","LEFT");
$this->db->join("locations as recLoc","recLoc.id=products.recLocation","LEFT");
I have two tables : This is the clients table.
+--------+---------+--------+------------+--------------+------------+
| id | name | debt | phone | address | products |
+--------+---------+--------+------------+--------------+------------+
| 1 | person1 | 55.54 | 8187654545 | Planet Earth | (none yet) |
| 1 | person2 | 55.64 | 8184256595 | Planet Mars | (none yet) |
+--------+---------+--------+------------+--------------+------------+
This is the products table:
+-----+----------+----------+----------+----------------+-------+
| id | category | name | type | size_in_ounces | price |
+-----+----------+----------+----------+----------------+-------+
| 1 | liquids | product1 | original | 2.7 | 1.99 |
| 1 | liquids | product2 | original | 7.7 | 5.99 |
| 1 | liquids | product3 | original | 10 | 7.99 |
+-----+----------+----------+----------+----------------+-------+
I am trying to add the name column from a row in the products table into the products column in the clients table. Basically what I am trying to accomplish is, on query, this to show in the clients table, and if possible for the clients debt column to increase in price accordingly.
+--------+---------+--------+------------+--------------+------------+
| id | name | debt | phone | address | products |
+--------+---------+--------+------------+--------------+------------+
| 1 | person1 | 63.49 | 8187654545 | Planet Earth | product3 |
| 1 | person2 | 57.63 | 8184256595 | Planet Mars | product1 |
+--------+---------+--------+------------+--------------+------------+
I tried to do this: but SQL gives me an error. I know the following code is wrong but it can give you an idea of what I'm trying to accomplish.
INSERT INTO `clients` (products) WHERE `name` = "person1"
SELECT `name` FROM `products` WHERE `name` = "product3";
And here is another silly thing; if I do this code :
INSERT INTO `clients` (products)
SELECT `name` FROM `products` WHERE `name` = "product3";
It creates a completely new client with every column null or empty except for the products column where it adds product3. Weird right?
I am creating a search portal in PHP from which user can search for a specific cuisine. In MySQL I have multiple tables for each cuisine and the respective hotel names that offer the cuisine. For example, in table
How can I query a specific cuisine table based on the cuisine search keyword?
So if a user enters 'mexican' as the search query, how can it connect to the 'Table2 - Mexican' and return the hotel names from this table?
Table1 - Chinese
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Table2 - Mexican
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Table3 - Pizza
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Your database concept is very unflexible. I think you should put the cuisines into your database as information (i.e. table content) instead of metadata describing single tables. Tables should generally considered to be static just like the code you write to access the database and its tables. If you implement the cuisines as different tables you would have to hardwire every cuisine into your code.
Here is a suggestion for a better approach:
Create a hotels table to store all the hotels,
Create a cuisines table to store all the different types of cuisines,
Make an additional table to establish the n:m relationship between the hotel and the cuisine.
Example:
hotels: id, name, address, city, telno, email
cuisine: id, name, description
rel: cuisine, hotel (where both are the foreign keys to the
id columns of the respective tables above)
See also:
How to handle a Many-to-Many relationship with PHP and MySQL.
MySQL: Many To Many Relationships ยป Return True
You might want to check this question to create a many-to-many relationship:
many-to-many and many-to-many intersections
I guess what you would like to achieve is something like this:
Table1 - Hotel
_______________________
| id | hotelname |
|______|______________|
| 1 | hotel1 |
| 2 | hotel2 |
| 3 | hotel3 |
| 4 | hotel4 |
| 5 | hotel5 |
|______|______________|
Table2 - Cuisine
____________________________________________
| id | cuisine_name | keywords |
|______|______________|____________________|
| 1 | Chinese | Shandong,Noodles,. |
| 2 | Mexican | Tacos,Beans,... |
| 3 | Itarian | Pizza,Pasta,.. |
|______|______________|____________________|
Table3 - HotelCuisine
___________________________________
| id | hotel_id | cuisine_id |
|______|____________|______________
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 3 |
|______|____________|_____________|
SQL:
SELECT hotelname, cuisine_name FROM Hotel
INNER JOIN HotelCuisine ON Hotel.id = HotelCuisine.hotel_id
INNER JOIN Cuisine ON Cuisine.id = HotelCuisine.cuisine_id
WHERE keywords like '%pizza%'
Result:
________________________________________
| hotelname | cuisine_name |
|_______________|______________________|
| hotel1 | Itarian |
| hotel3 | Itarian |
|_______________|______________________|
DEMO: http://sqlfiddle.com/#!2/961de/1
Hope this helps
you can check SQL UNION. But instead of having multiple tables with the same fields, you can try normalization to minimize the redundancy and to make queries easier.
Something like:
Hotel Table
-----------------------------
id | hotelname | categoryID
------------------------------
1 | hotel name 1 | 1
2 | hotel name 2 | 2
-----------------------------
Category Table
-------------------
id | categoryname
-------------------
1 | chinese
2 | mexican
------------------
And query as simple as:
SELECT a.hotelname, b,categoryname
FROM hotel_table a
LEFT JOIN category_table b
ON a.categoryID = b.id AND b.categoryname LIKE '%mexican%';
I have a table like the one below that currently has no values for rating, lib_id or votes.
library
id | title | year | rating | votes | lib_id |
---------------------------------------------
1 | book1 | 1999 | | | |
2 | book2 | 2010 | | | |
3 | book3 | 2009 | | | |
4 | book4 | 2007 | | | |
5 | book5 | 1987 | | | |
I then have the classifications table which looks like this.
classifications
id | title | year | rating | votes | lib_id |
---------------------------------------------
108 | book154 | 1929 | | | |
322 | book23 | 2011 | | | |
311 | book3 | 2009 | 9.3 | 4056 | 10876 |
642 | book444 | 2001 | | | |
533 | book567 | 1981 | | | |
It can happen that entries in the library table may not appear in the classifications table and vice-versa. There can also be the possibility that the title of the book is not unique. So what I want to do is go through each row in the library table, take the title and year columns, go to the classifications table and find the row that has these two values, retrieve the corresponding rating, votes and lib_id columns and update the entry in the library table.
I also want to use PDOs. Below is a non-working example of what i'm trying to achieve.
$update_vals_STH =
$DBH->prepare(
"UPDATE library SET lib_id=?, rating=?, votes=?
FROM (SELECT lib_id, rating, votes)
FROM classifications WHERE title=? AND year=?";
Any help would be appreciated. I'm quite new to MySQL and have been struggling with this one for a while.
You can join tables on update statement too.
UPDATE library a
INNER JOIN classifications b
ON a.title = b.title AND
a.year = b.year
SET a.rating = b.rating,
a.votes = b.votes,
a.lib_id = b.lib_id
// WHERE clause // if you want to have extra condition.
SQLFiddle Demo
UPDATE
For better performance, you need to add indexes on the following field.
ALTER TABLE library ADD INDEX (title, year);
ALTER TABLE classifications ADD INDEX (title, year);