I have a orders table with the following columns:
-userid
-orderid (primary key)
-date
-name
-qty
So I would like to display in tables in a summary page all orders and group them under each userid. So it would be like
Userid: 0001
| Orderid | Name | QTY |
| 1001 | Item A | 10 |
| 1002 | Item B | 5 |
Userid: 0003
| Orderid | Name | QTY |
| 1003 | Item C | 6 |
| 1004 | Item C | 7 |
So far I've experimented and got:
display in a single table all the items ordered by userid
with a GROUP BY userid, I've managed to create as many tables as there are different userid
But I can't seem to combine the two into the desired results and would really appreciate some advice on what I am doing wrong.
Thanks in advance and I hope my explanation made sense!
I think you will need to do this in PHP
Query with
select * from orders order by userid, orderid
In PHP something like this psudo code
$this_user_id=0;
for (each order line)
{
get fields from db result;
if ($this_user_id!=$userid)
{
$this_user_id=$orderid// save change of userid flag
print user heading;
}
print order lines;
}
Related
I have tree tables:
table user_products
user_id | product_specific_id | order_no
1 | 1 | 1
1 | 2 | 1
1 | 3 | 2
table products_library
product_specific_id | product_id
1 | 3
2 | 3
3 | 1
table product_names
product_id | name
1 | prod1
2 | prod2
3 | prod3
Every product in the database does have unique product_id. But when user does order any product, he can modify it, so I created product_specific_id that I'm using in user_products, and table products_library where I can translate every unique product_specific_id to the base product_id.
Now every product_specific_id does have name of related product_id, that I do store in table product_names.
Now I need to display the name of every product_specific_id for specific user_idand order_no.
Expected result should look like this:
user_id | order_no | product_specific_id | name
1 | 1 | 1 | prod3
1 | 1 | 2 | prod3
I'm able to fit in two queries: first I'm selecting list of product_specific_id from user_products, and than I'm nesting SELECT like
SELECT name
FROM product_names
WHERE product_id IN (SELECT product_id
FROM products_library
WHERE product_specific_id IN (...)
But is it possible to fit everything it in one query? I have no idea how such thing could be achieved, if at all. Also, I'm not sure if nesting queries like this is good or not in the first place. Perhaps is it just fine to get it in two queries and nesting queries too much is bad idea?
You can try the below way using just JOIN
select user_id,order_no,u.product_specific_id,name
from user_products u join products_library p on u.product_specific_id=p.product_specific_id
join product_names pn on p.product_id=pn.product_id
where user_id=1 and order_no=1
I have a table with three columns, id, name, and value, as shown below. I want to count the average where the id and value columns are the same, how can I do this?
+----+--------+-------+
| id | name | value |
+----+--------+-------+
| 2 | rahmat | 3 |
| 2 | olive | 5 |
| 3 | sari | 3 |
| 3 | ryan | 2 |
| 1 | zaki | 1 |
+----+--------+-------+
Try using this query:
SELECT AVG(value)
FROM table
WHERE id = value
The output from the sample table you gave in your OP would be 1.5, since sari and zaki are the only 2 users whose records have id and value columns which are equal.
according to your question yes you need to use
SELECT AVG(value)
FROM #table
WHERE id = value
I have created a sqlfiddle here
http://sqlfiddle.com/#!3/9eecb7/4105
from the nature of this question I feel you trying to calculate average of values of those rows having same ids. If that's the case I have created another fiddle http://sqlfiddle.com/#!3/9eecb7/4110 where you need to use group by
select id, sum(value)/count(id) as average from #table group by id
Lemme know if it is something you are after or you need something else.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have three tables and I would like to select from the tables without producing duplicates.
The table are as follows:
Customers
id | name | lastName
---------------------------------------
1 | john | doe
2 | helen | keller
Orders
The userID column is a foreign key that references John Doe, so John orders 3 items.
id | userID | order
---------------------------------------
1 | 1 | pizza
2 | 1 | pasta
3 | 1 | lasagna
CustomerRating
The userID column is a foreign key that references John Doe, so John leaves 5 reviews.
id | userID | rating | comment
-------------------------------------------------
1 | 1 | 5/5 | was good
2 | 1 | 5/5 | excellent
3 | 1 | 4/5 | great
4 | 1 | 4/5 | great
5 | 1 | 4/5 | great
How would I select from the 3 tables where I can get a return results that look like this?
id | name | lastName | order | rating
-----------------------------------------------------------------
1 | john | doe | pasta | 5/5
| | | pizza | 5/5
| | | lasagna | 4/5
| | | | 4/5
| | | | 4/5
I've tried joining these tables, but since John has left 5 reviews and only ordered 3 times, the id, name,lastName, and order columns gets filled with duplicate data.
Thanks!
I don't have any experience in MySQL but I assume that it works similar to MSSQL.
So the format in which you are expecting the output is not possible. You can rather get the order and rating column values as comma separated
Here is a similar kind of question that might help you
including example based on link
try something like this
SELECT Customers.id, Customers.name, Customers.lastName,
GROUP_CONCAT(Orders.order) OrderedDishes,
GROUP_CONCAT(CustomerRating.rating) RatingsGiven
FROM
..... rest of your query .....
There are ways to discard duplicates (SELECT DISTINCT, UNION, GROUP BY) but it is not clear whether users update existing rating or create new ones. And what you want to see: the last rating or the average one
On the other note - i would change your entire setup:
order table would contain order_id, customer_idand other order related stuff like order_date
products table that would describe each of your dishes and their info like price, description etc
order_products table with fields order_id and prduct_id
if users rate products then your rating table would need at least product_id, customer_id, rate_value. I'd also add ratingDate That way you can get averages or select the last one by Max(ratingDate)
I think you need to add an orderID field to the CustomerRating table else there is no way to relate an item to its rating.
OK, so this might sound complicated, let me explain... i've been banging my head on this for a while, and i'm stuck in a loop now... can't figure it out!
T1 is a simple alert data table. It's got theses headers :
| ID | TITLE | DATA |
---------------------------------
| 1 | Title1 | Text1 |
| 2 | Title2 | Text2 |
T2, is just a simple user table
| ID | Name |
-------------------
| 1 | Fred |
| 2 | Bill |
| 3 | Brad |
T3 is a link table between T1 and T2. Basically, the first time a user (T2.ID) views an alert (T1.ID) he hasn't viewed, his ID is added to this table so I know he's viewed it.
| ID | T1ID | T2ID |
--------------------------
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 2 |
| 5 | 2 | 3 |
| 6 | 3 | 1 |
| 7 | 3 | 3 |
I can tell user 1 has not viewed alert 2 nor has user 2 viewed alert 3. So next time they login, I should popup these alerts to them... So if i'm user 1, when I login my admin site, I want MySQL to tell me I havn't viewed alert 2, then UPDATE that table when I do.
As I sayed, T3 should build up everytime a user views an alert where his ID does not match an alert ID. I feel, i'm so damn close...
Simple enough, now how do I do it? I ain't going to post what doesn't work...
Please help...
The first thing, you want to start with the alert table as it is applicable to all uers. From there, doing a LEFT-JOIN to the already viewed table specifically for the login user ID in question AND the alert IDs are the same.
The where clause is looking ONLY for those entries that it CAN NOT find the corresponding login user ID as having viewed the alert. For those, get the alert's title and data.
After that, and presentation to the user, you can insert after the alerts have been viewed.
I would ensure the already viewed table has an index on both keys (T1ID, T2ID) to help optimize the join
SELECT
T1.Title,
T1.Data
from
YourAlertTable T1
LEFT JOIN AlreadyViewedTable T3
ON T3.T2ID = TheUserIDParameterWhoLoggedIn
AND T1.ID = T3.T1ID
where
T3.T2ID IS NULL
try :
Select * from t1 where id not in (select t3.t1id from t3 where t3.t2id = <loggedin userid>)
I am new to the world of mysql and I'm having some trouble getting the data I need from a database.
The 2 tables I have are...
Results
ID | TITLE | LOTS OF OTHER DATA |
1 | res1 | |
2 | res2 | |
3 | res3 | |
4 | res4 | |
5 | res5 | |
Categories
ID | RESULT_ID | CATEGORY NAME |
1 | 1 | purchase |
2 | 1 | single_family |
3 | 1 | conventional |
4 | 2 | usda |
5 | 3 | somecategory |
I'm trying to create a query that will select results that belong to all of the categories provided in the query. For example a query for purchase & single_family & conventional in this example would return the first result in the results table.
Does that make sense? Is there a query that will do this or is this more of a problem with my database structure?
Thanks a lot!
Try something like this:
SELECT * FROM Results r
INNER JOIN Categories c on r.ID = c.RESULT_ID
WHERE c.name in ('purchase', 'single_family', 'conventional')
GROUP BY r.ID
HAVING COUNT(c.ID) = 3
The basic select with join will get you three rows only for result 1.
Edit: To make sure your code won't break if you change your database you should always select the fields you want explicitly: SELECT r.ID, .. FROM ..
So you're basically doing a simple join with all the category table for all categories where the category name is one of the names in the list. Try to run the 3 first lines manually to see the result you get.
Next you group by the result id. This means that you are aggregating all the rows sharing the same result id into one row. The last line means that we are filtering the aggregated columns that are aggregated by 3 rows. That means that you will only return results that have 3 matching categories.
So the only problem with this approach is if you have duplicate result_id, categoryname in your Categories table.