There is lot of questions on merging SQL results. Here is my case that has two different SQL results.
userID Car
1 Ford
2 Honda
3 Toyota
userID Color
1 Red
2 Silver
3 White
Is there a way to merge both of the above into the follows in SQL:
userID Car Color
1 Ford Red
2 Honda Silver
3 Toyota White
This looks like a join:
select ca.user_id, ca.car, co.color
from cars ca join
colors co
on ca.user_id = co.user_id;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.
Improve this question
I have 5 tables involved in SQL.
conversation_id
staff_id
client_id
summary
1
1
1
conversation between John and Red
1
1
2
conversation between John and Orange
1
2
3
conversation between Kitty and Yellow
1
2
4
conversation between Kitty and Green
staff_id
staff_name
1
John
2
Kitty
client_id
province_id
client_name
1
35
Red
1
81
Orange
1
81
Yellow
1
81
Green
province_id
province_name
35
Toronto
81
Vancouver
division_id
staff_id
province_id
1
1
35
1
1
35
1
2
81
Kitty SHOULD NOT have access to conversation between John & Toronto's Client (client red) because she doesn't have access to Toronto (as shown in the division table).
This is the result from code below:
staff_name
province_name
client_name
summary
John
Vancouver
Red
conversation between John and Red
John
Vancouver
Orange
conversation between John and Orange
Kitty
Vancouver
Yellow
conversation between Kitty and Yellow
Kitty
Vancouver
Green
conversation between Kitty and Green
Here's my code.
SELECT
conversation.conversation_id,
conversation.staff_id,
conversation.client_id,
conversation.summary,
staff.name as staff_name,
client.name as client_name,
province.name as province_name
FROM conversation
LEFT JOIN staff
ON staff.staff_id = conversation.staff_id
LEFT JOIN client
ON client.client_id = conversation.client_id
LEFT JOIN province
ON province.id = client.company_province_id
INNER JOIN division
ON division.province_id = client.province_id
WHERE staff_id = 2
What is wrong?
How I can achieve my goal of making the first row disappear?
I want to query the database:
If two orders in a row are the same then ignore the second one. I used Distinct but I have many orders with the same name with different products so I want to ignore the next order the row with the same name in the row
order_id customer_name product_name
234 Jhon Mango
235 Jhon Mango
236 Marvel tomato
237 Paul Potato
238 Jhon Banana
I am trying to add titles or breaks between groups of names in MYSQL. Here's an example.
Output:
Bob Honda 1
Bob Acura 2
Bob Toyota 3
Joe Honda 1
Joe Acura 2
Joe Toyota 3
Jim Honda 1
Jim Acura 2
Jim Toyota 3
What I want is:
Bob
Bob Honda 1
Bob Acura 2
Bob Toyota 3
Joe
Joe Honda 1
Joe Acura 2
Joe Toyota 3
Jim
Jim Honda 1
Jim Acura 2
Jim Toyota 3
or even just this
Bob Honda 1
Bob Acura 2
Bob Toyota 3
Joe Honda 1
Joe Acura 2
Joe Toyota 3
Jim Honda 1
Jim Acura 2
Jim Toyota 3
Is this possible through MySQL?
Thank you in advance.
Suppose initially you have a query like this
select name,
car,
number
from a_table
You can use something like this
select name,
car,
number
from a_table
union all
select name,
'' as car,
1000000000 as number
from a_table
group by name
order by name, number
here grouped results generates the extra lines and then order by fixes the rows orders.
I have a mysql table that has several values like this
id name number value
------------------------------------
1 John 3 blue
1 John 3 red
1 John 3 green
2 Aly 2 red
2 Aly 2 blue
3 Sam 1 green
4 Tiad 6 white
5 Krix 5 orange
Is there a SQL command that can group by or combine these values into one row, with the final column's values put into that one row as values separated by commas? So basically, what command could take the above table and change it into
id name number value
------------------------------------
1 John 3 blue, red, green
2 Aly 2 red, blue
3 Sam 1 green
4 Tiad 6 white
5 Krix 5 orange
Is there such a command?
you can try sommething like this
Updated:
SELECT id,name,number, GROUP_CONCAT(value SEPARATOR ', ') AS value
FROM yourtable GROUP BY id
I know just about the basic usage of COUNT(*) and I wonder if I can use it or some other function to get the following result.
I have a table with people and the products they have purchased (product_id_). I have second table which maps each product_code to a single product_category.
Using a simple SELECT I can combine both tables to get:
first last product_code product_category
John BGood 100 Food
John BGood 29 Beverage
John BGood 30 Beverage
Rita Black 25 Fashion
Betty Rock 36 Electronics
Betty Rock 72 Food
Betty Rock 100 Food
Betty Rock 36 Electronics
But what I would like is to count for each person the number of products it purchased from each category. product_category is an enum with 5 possible values (the four above and Other). I would like to get a table like:
first last product_category count
John BGood Food 1
John BGood Beverage 2
John BGood Fashion 0
John BGood Electronics 0
John BGood Other 0
Betty ...
SELECT first, last, product_category, COUNT(product_code)
FROM <table>
ORDER BY last, first
GROUP BY first, last, product_category
Try this query
SELECT first, last, product_category, count(product_category)
FROM <table_name>
GROUP BY product_category
Append GROUP BY person_id, product_category to your SELECT.