output content of a table as one line in mysql sql - mysql

I need to output a purchase order into one line that has product and quantity of each product.
say
select product, quantity from order;
outputs
product A , 2
product B , 3
product C , 7
I need the output to be in one line:
product A,2,product B,3,product C, 7
Can this be achieved using mysql sql ?

try this:
SELECT GROUP_CONCAT(product,',' ,quantity) FROM ORDERS;
see the sqlfiddle:
http://sqlfiddle.com/#!2/d18e6/7

Check this Demo SQLfiddle
Query
SELECT GROUP_CONCAT(product, ',' ,quantity,'') FROM order1;
Result
|------------------------------------------------|
|GROUP_CONCAT(PRODUCT, ',' ,QUANTITY,'') |
|------------------------------------------------|
|product A,2,product B,3,product C,7 |
|------------------------------------------------|

Related

Select similar records from mysql

I have a database as shown below
ID color number code
102966 red 1 9f6606069f9b999b
102968 red 1 9f6606069f9b999b
102967 red 1 9f0606069f9f9f9f
102969 red 1 9f0606069f9f9f9f
103630 red 1 bbff9f0f8fdc9f7e
101582 red 1 bbff9b0fcf9f99d9
102000 red 1 99fd9f0fab999fff
101603 red 1 bbff9f0d8f9d96df
102016 red 1 bbff9900c09999df
This table has more then 4000 entries.
I got this output by using
Select * from mytable order by code asc
Now I want output as a Id-Id-Id.... where code is similar.
So for given snippet it should come like
102966-102968
102967-102969
So I want like that those records are similar, their code should come like this.
Please help.
I don't really see what your last result row is about, it might be a mistake or I don't understand what you need
I think you are looking for a group_concat
select group_concat(ID ORDER BY ID SEPARATOR '-' ) AS dup
from mytable
GROUP BY code
HAvING count(*) >1
ORDER BY dup
Results:
| dup |
|---------------|
| 102966-102968 |
| 102967-102969 |
A group_concat shows all the values matching the GROUP BY, here the ID values.
The term SEPARATOR is there to specify - as separator between your IDs, because the default separator is ,
If you want all rows, even those for which there are no duplicate code, remove the having clause
SQL Fiddle
the last answer from #Thomas G is correct but if you want to have it in the correct order use:
select CONCAT(MIN(ID),'-',MAX(ID))
from mytable
GROUP BY code
HAvING count(*) >1

MYySQL is the Table Relationship wrong?

I'm very new in Databases and more specific in MYSQL. I use xampp + MySQL Workbench.
I make 3 tables using MySQL Workbench:
- tbStores with fields StoreID(PK-INT-AI), StoreName
- tbProducts with fields ProductID(PK-INT-AI), ProductName
- tbProductDetails with fields ProductDetailID(PK-INT-AI), Price, ProductID(FK), StoreID(FK)
*PK=Primary Key
*INT=Numeric Type Attributes
*AI=Auto Increments
In case you don’t understand the Relationships above:
1 to many From tbStores(StoreID) To tbProductDetails (StoreID)
1 to many From tbProducts(ProductID) To tbProductDetails (ProductID)
I add values to the fields:
- tbStores=> StoreName=> Store 1
- tbProducts=> ProductName=> Product 1, Product 2
- tbProductDetails=> Price=> 50, 30
- tbProductDetails=> ProductID=> 1, 2
- tbProductDetails=> StoreID=> 1, 1
To the Query:
SELECT tbStores.StoreName, tbProductDetails.Price, tbProducts.ProductName
FROM tbStores, tbProductDetails, tbProducts
Where ProductName = 'Product 1';
The Problem:
Query will return this
Store 1, 50, Product 1
Store 1, 30, Product 1
Is giving me Same Product with 2 different Prices.
What I was expecting to take was this :
Store 1, 50, Product 1
What am I doing wrong? I believe it has to do with relationships but I can't figure it out. Thanks
You need to join the tables together (specify how they are related) in the query, the query should look something like this:
SELECT tbStores.StoreName, tbProductDetails.Price, tbProducts.ProductName
FROM tbProductDetails
JOIN tbStores ON tbStores.StoreID = tbProductDetails.StoreID
JOIN tbProducts ON tbProducts.ProductID = tbProductDetails.ProductID
WHERE tbProducts.ProductName = 'Product 1';
If you want all products you have to remove the where clause. Note that I took the liberty of changing your implicit joins in the from clause to explicit joins using the join keyword.
Sample SQL Fiddle
Sample output:
| STORENAME | PRICE | PRODUCTNAME |
|-----------|-------|-------------|
| Store1 | 50 | Product1 |
What you want is to use JOIN combined with ON
SELECT StoreName, Price, Product Name
FROM tblStores
JOIN tblProduct ON tblStores.StoreID = tblProducts.StoreID
JOIN tblProductDetails ON tblProduct.ProductID = tblProductDetails.ProductID
WHERE ProductName = 'Product 1'
You may consider GROUP BY to identify the specific stores.

Calculate total of some fields in mysql?

I have a table like this
item_id item_quantity
----------------------
324 2
432 23
543 12
879 3
The item_id field is not auto-increment but it is unique.
The inputs of the query will be some item_id values and the result should be sum of those in item_quantity.
For instance, if the inputs are 324, 543, then the query should return 14 (2 + 12)
Is it possible to do this at mysql level?
Try using in :
SELECT SUM(item_quantity)
FROM table
WHERE item_id in (324,543)
This works on MSSQL, not sure about MySQL, though:
SELECT SUM(item_quantity) FROM MyTable WHERE item_id in (324, 543)
EDIT: OK, others got the same answer. So I guess it also works on MySQL.
If all you need is the sum of the item_quantity, all you need to do is:
Select Sum(item_quantity)
From Table
Where item_id In (324, 543)

Using 'AND' in SQL WHERE clause for the same filed

Here is the scenario. I have 1 table that has all the contact details. Another table that has all the list of Categories. And a 3rd table which is an associate table, that has the ID of the first table and the ID of the second table.
This is how my associate table looks like
contactdid -2 | categoryid -1
contactdid -2 | categoryid -2
contactdid -2 | categoryid -3
contactdid -3 | categoryid -1
contactdid -3 | categoryid -3
This is my SQL code below(Generated using SQLyog and i included the where clause).
SELECT
press_contacts.email
FROM
contacts_category
INNER JOIN press_category
ON (contacts_category.categoryid = press_category.id)
INNER JOIN press_contacts
ON (contacts_category.contactdid = press_contacts.id)
WHERE contacts_category.categoryid = 1 AND contacts_category.categoryid = 2 ;
I get the output when I do not have AND contacts_category.categoryid = 2inserted in the code.
Any idea how to solve this.I clearly have data.
Thanks in advance for the help.
contacts_category.categoryid can not be 1 and 2 at the same time, perhabs you mean OR instead of AND?
Use OR or IN() instead of AND.
A field can't have two values at the same time
If you only want to see those email addresses with contacts in both categories, try:
SELECT press_contacts.email
FROM contacts_category
INNER JOIN press_contacts
ON (contacts_category.contactdid = press_contacts.id)
WHERE contacts_category.categoryid in (1, 2)
GROUP BY press_contacts.email
HAVING COUNT(DISTINCT contacts_category.categoryid)=2

mysql select update

Got this:
Table a
ID RelatedBs
1 NULL
2 NULL
Table b
AID ID
1 1
1 2
1 3
2 4
2 5
2 6
Need Table a to have a comma separated list as given in table b. And then table b will become obsolete:
Table a
ID RelatedBs
1 1,2,3
2 4,5,6
This does not rund through all records, but just ad one 'b' to 'table a'
UPDATE a, b
SET relatedbs = CONCAT(relatedbs,',',b.id)
WHERE a.id = b.aid
UPDATE: Thanks, 3 correct answers (marked oldest as answer)! GROUP_CONCAT is the one to use. No need to insert commas between the ids using relatedids = CONCAT(relatedids,',',next_id) that is done automatic by GROUP_CONCAT.
You'll have to use the mysql group_concat function in order to achieve this: http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_group-concat
Look into GROUP_CONCAT(expr)
mysql> SELECT student_name,
-> GROUP_CONCAT(DISTINCT test_score
-> ORDER BY test_score DESC SEPARATOR " ")
-> FROM student
-> GROUP BY student_name;
You can't do that in standard SQL. You could write a stored procedure to do that. I had a similar problem, but I was using PostgreSQL so I was able to resolve it by writing a custom aggregate function so that you can do queries like
select aid, concat(id)
from b group by
aid
Update: MySQL has a group_concat aggregate function so you can do something like
SELECT id,GROUP_CONCAT(client_id) FROM services WHERE id = 3 GROUP BY id
as outlined here.