I've a mysql table named PRODOTTO structured in this way
+----+-----------+-------------+----------+--------+
| id | categoria | prodotto | quantita | prezzo |
+----+-----------+-------------+----------+--------+
| 1 | bar | Maxibon | 887 | 2.00 |
| 2 | bar | Limonata | 21 | 1.50 |
| 3 | bar | Coca Cola | 70 | 1.50 |
| 4 | bar | chupa chups | 30 | 0.60 |
| 5 | bar | pirulo | 79 | 1.00 |
+----+-----------+-------------+----------+--------+
and another one named CONSUMAZIONI
+----+----------+----------+--------+-----------------+--------+
| id | prodotto | quantita | prezzo | totale_parziale | status |
+----+----------+----------+--------+-----------------+--------+
| 1 | Maxibon | 1 | 2.00 | 2.00 | open |
| 2 | pirulo | 6 | 1.00 | 6.00 | open |
+----+----------+----------+--------+-----------------+--------+
I'd like to merge this to tables and obtain a unique table like this
+----+----------+----------+----------------------+--------+----------------+--------+
| id | prodotto | quantita | quantita_disponibile | prezzo | totale_parziale | status |
+----+----------+----------+----------------------+--------+-----------------+--------+
| 1 | Maxibon | 1 | 887 | 2.00 | 2.00 | open |
| 2 | pirulo | 6 | 79 | 1.00 | 6.00 | open |
+----+----------+----------+----------------------+--------+-----------------+--------+
So the new table is the combiantion fo the table CONSUMAZIONI and the column "quantita" of the prodotto's table.
On this new table I will do a query like this
SELECT * FROM...
Try this:
CREATE TABLE Fusione AS
(SELECT p.id,p.prodotto,c.quantita,p.quantita AS quantita_disponibile,p.prezzo,c.totale_parziale,c.status
FROM Prodotto P, Consumazione c
WHERE p.id=c.id
)
Also consider to restucture your schema, because you have redundancy in the two tables. Indeed, table Consumazioni should be a relation on table Prodotto. So Consumazioni should be someting like:
id_prodotto | quantità | status
The other information are available by joining the two tables and performing some basic mathematical operations on data (e.g., totale_parziale is prezzo*quatita).
What you can do in PHP, or rather in MySQL is Create a VIEW; so you don't always have to make a JOIN.
You can create a view for those tables, like the following:
CREATE VIEW prodConsumption AS
SELECT
p.prodotto, c.quantita, p.quantita AS quantita_disponibile, p.prezzo, c.totale_parziale, c.status
FROM
PRODOTTO p
INNER JOIN
CONSUMAZIONI c ON p.prodotto=c.prodotto
Ofcourse, you should look into your table, joining by name is horrible, and your IDs doesn't seem to match.
Afterwards, you can do as you wanted:
SELECT * FROM prodConsumption
You can create a VIEW for this:
CREATE VIEW view_prodotto_consumazinoni AS
SELECT
c.id AS id,
c.prodotto AS prodotto,
c.quantita AS quantita,
p.quantita AS quantita_disponibile,
c.prezzo AS prezzo,
c.totale_parziale AS totale_parziale,
c.status AS `status`
FROM consumazinoni c INNER JOIN prodotto p ON c.prodotto=p.prodotto;
This procudes exactly the output you want:
SELECT * FROM view_prodotto_consumazinoni;
id prodotto quantita quantita_disponibile prezzo totale_parziale status
1 Maxibon 1 887 2 2 open
2 pirulo 6 79 1 6 open
The advantage of such a VIEW is that you can define it once and then use it just like an ordinary table without having to think about the JOIN done all the time. It is synchronized with the changing data in the two base tables, so always up to date.
I set up a sql fiddler for you, so that you can play around a bit:
http://sqlfiddle.com/#!9/76a43/1
Have fun!
Related
I have three tables group_sentences, group_sentences_attributes and group_senteces_categories.
I have an attributes array which I am using in query with IN (after implode).
Then I have one category ID because they are stored recursively, so no need for an array.
I need to select one group number where is the biggest match for $attributesArray and of course category too.
Here is table group_sentences_attributes
+-----+-------+-----------+
| id | group | attribute |
+-----+-------+-----------+
| 1 | 1 | 3564 |
| 2 | 1 | 3687 |
| 3 | 1 | 3689 |
| 4 | 2 | 3687 |
| 5 | 2 | 3564 |
+-----+-------+-----------+
Here is group_sentences_category
+-----+-------+----------+
| id | group | category |
+-----+-------+----------+
| 1 | 1 | 1564 |
| 2 | 1 | 1221 |
| 3 | 1 | 1756 |
| 4 | 2 | 1358 |
| 5 | 2 | 1125 |
+-----+-------+----------+
Here is my query, but I am afraid that it won't do the job done.
SELECT group_categories.group
FROM group_categories, group_attributes
WHERE group_categories.category = '$category'
AND group_attributes.attribute IN ($attributesArray)
GROUP BY group_categories.group
ORDER BY count(group_attributes.attribute)
Any help would be appreciated, thanks.
First, the table in your query do not match the tables in the question. I am guessing they are simply missing the "sentence". Then, you have no join clause. Simple rule: Never use commas in the from clause.
group is a lousy name for a column, because it is a keyword in SQL. The following may be what you are looking for:
SELECT gc.groupid
FROM group_sentences_attributes sa JOIN
group_sentences_category sc
ON sa.groupid = sc.groupid
WHERE sc.category = '$category' AND
sa.attribute IN ($attributesArray)
GROUP BY sa.groupid
ORDER BY count(sa.attribute);
If you only want one row, then add LIMIT 1 to the end.
I would like to create and UPDATE MySQL query based on a SELECT statement that is already working.
So I have the following select statement that joins two tables - tbl_random and tbl_products by finding a random record from the second table:
$sql_select = "SELECT tbl_random.keyword, tbl_random.model_id,
tbl_products.make, tbl_products.model
FROM tbl_random LEFT OUTER JOIN
tbl_products
ON tbl_random.model_id = tbl_products.model
GROUP BY tbl_random.keyword
ORDER BY RAND()";
$rs_select = $db -> Execute($sql_select);
This is how tbl_random should look like after the update:
+---------+------------+---------+---------+--------------+
| keyword | model_id | make | model | more_data1 |
+---------+------------+---------+---------+--------------+
| apple1 | 15 | app5 | 15 | data1 |
| apple2 | 15 | app1 | 15 | data2 |
| pear | 205 | pear53 | 205 | data3 |
| cherry | 307 | cher74 | 307 | data4 |
| melon | 5023 | melo2 | 5023 | data5 |
+---------+------------+---------+---------+--------------+
What UPDATE query should I use in order to the able to update the make and model fields in tbl_random, with some respective random values from tbl_products?
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 big filter with many options and want to generate the query for sql automaticle and without many code.
GET:
searchvalue=abc
&title=abc
&description=abc
&category=1
&subcategory=2
&zip=7
&city=ke
&country=DE
SQL:
SELECT activity.* FROM activity,subcategory,city,country
WHERE activity.title LIKE '%abc%' OR activity.description LIKE '%abc%'
AND subcategory.SubID = 2
AND city.zip LIKE '%7%'
AND city.City LIKE '%ke%'
AND country.CShort= 'DE'
With this options, I have 1 row in my database.
The answer is this row many times, many many times.
I know that the sql duplicate a row, when a table is not used in a WHERE clausel - but why he do it now and how can I solve that?
Edit: I have a ER, but the database is in german (school project), maybe it help you to understand:
Thanks!
You are doing a cross product by selecting multiple tables. SQL will return every row from the one table combined with every row in the other table.
For example in a database with table a
|------|----------|
| idA | textA |
|------|----------|
| 1 | fooA |
| 2 | barA |
|------|----------|
and table b
|------|----------|
| idB | textB |
|------|----------|
| 1 | fooB |
| 2 | barB |
|------|----------|
when you do
SELECT * FROM a, b
you would get
|------|----------|------|----------|
| idA | textA | idB | textB |
|------|----------|------|----------|
| 1 | fooA | 1 | fooA |
| 1 | fooA | 2 | barA |
| 2 | barA | 1 | fooB |
| 2 | barA | 2 | barB |
|------|----------|------|----------|
To combine these rows logically you do a JOIN. That means you tell in your query which rows belong together. You can do so by JOIN clause or without JOIN clause directly in the WHERE clause.
Back to the example you would do
SELECT * FROM a, b
WHERE a.idA = b.idB
-- or
SELECT * FROM a
JOIN b ON a.idA = b.idB
you would get only 2 rows.
|------|----------|------|----------|
| idA | textA | idB | textB |
|------|----------|------|----------|
| 1 | fooA | 1 | fooA |
| 2 | barA | 2 | barB |
|------|----------|------|----------|
To answer your question:
You have to support JOIN/WHERE clauses to connect your tables activity, subcategory, city and country according to your database schema.
I don't know your table structures but for example clauses like this:
WHERE
...
AND city.country_id = country.id
AND activity.subcategory_id = subcategory.id
AND ...
I have a table of Products that looks like so:
| id | Description | Price |
| 1 | dinglehopper | 2.99 |
| 2 | flux capacitor | 48.99 |
| 3 | thing1 | 48.99 |
And so on...
Then I have an OrderLineItem table which, as you can guess, links each item in an order to the product:
| id | productID | OrderID |
| 43 | 1 | 12 |
| 44 | 2 | 12 |
| 52 | 3 | 15 |
So, as you can see, order #12 contains a dinglehopper and flux capacitor. How can I get this information in a single query? I just want ALL the products associated with a given OrderID in the OrderLineItem table.
May be by
select p.description,p.id,o.irderId
from
`orderLineItem` o, `product` p
where
p.id = o.productId;
or
select p.description,p.id,o.irderId
from `orderLineItem` o
join `product` p
on p.id = o.productId;
LEFT JOIN :)
http://www.w3schools.com/sql/sql_join_left.asp
#Pete About "single" query part, you should make VIEW from this join, if really going to use a lot.