How to select different columns as a list? [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a database like this:
column1 column2 column3
15 21 19
17 12 13
15 24 14
Now I want to get a list of every single number like this:
x value
1 15
2 21
3 19
4 17
5 12
...
Thank you!

Try this:
SELECT (#auto:=#auto+1) AS num, val
FROM (SELECT column1 AS val
FROM tableA
UNION
SELECT column2 AS val
FROM tableA
UNION
SELECT column3 AS val
FROM tableA
) AS A, (SELECT #auto:=0) AS B

Related

MYSQL - calculate values based on other ID [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
ItemID Quantity CustID
1 1 1000
6 1 1000
7 2 1000
2 12 1001
3 24 1001
4 16 1001
2 1 1002
how should I calculate the quantity based on each custID purchased?
i.e.
CustID Quantity
1000 4
For a particular customer ID you can use
select custid, sum(quantity)
from table_name
where custid=1000
Or for all customer id's you can use
select custid, sum(quantity)
from table_name
group by custid
Fiddle
Seems like a simple SUM:
SELECT CustID,SUM(Quantity) as Quantity
FROM TableName
GROUP BY CustID
You need to learn more about aggregate functions. Read more here.

Select distinct word count? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to put together a report that shows what search criteria a user used.
Right now it is basic and uses SELECT DISTINCT and then GROUP BY.
The problem is we would like to see this broken down by words too. The phrase criteria is useful but we would like to see:
Searches:
red apples are good
yellow bananas are bad
bad apples are not bananas
pears are not red
What we would like to see:
red 2
apples 2
are 4
good 1
yellow 1
bananas 2
bad 2
pears 1
not 2
I should note too that we have too me search terms to go through and write seperate %LIKE statements for them - and they change.
try this example
SELECT word,COUNT(*) as count
FROM
(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(searches,' '),' ',value.v),' ',-1) as word
FROM yourtable,(SELECT 1 as v UNION
SELECT 2 UNION
SELECT 3 UNION
SELECT 4 UNION
SELECT 5)value
)T
WHERE word != ''
GROUP BY word
sqlFiddle

MySql query to count song votes in music chart [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have 4 tables for a music voting site.
Songs -id.
Charts - id.
Chart_song - id, song_id, chart_id. (join table)
votes - id, song_id_fk, chart_id_fk.
I want to count how many votes each song has for a particular chart (id = 4)
Here is how the vote table looks when people vote for a song (id = 1) to chart id (4)
id | song_id_fk | chart_id_fk |
1 | 1 | 4 |
2 | 1 | 4 |
Hopefully u understand. Please help. If I pass 'WHERE chart_id_fk = 4', I want to get a count of 2 for song_id_fk = 1.
I guess you are looking for something like
select count(*) as vote_count, song_id_fk, chart_id_fk from votes group by song_id_fk, chart_id_fk

A Tricky SQL query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this table with 2 columns of interest: ID, Memo_No plus some other columns.
ID is primary key of int type and auto-increment. Memo_No. is also int.
I need query, such that if memo no. is occuring only once in the table, the ID associated with it have to be selected.Plus, if same Memo has two different ID's, ID which is greater has to be selected.
For example i have table like this:
ID || Memo_No
1 2
2 3
3 4
4 5
5 4
6 6
7 2
From above table, I want to select rows whose ID's are 2,4,5,6,7 .
Thanks.
This is quite easy as an aggregation:
select max(id), memo_no
from t
group by memo_no;

Query to count frequency [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have table of the form:
A 2
A 2
A 2
A 2
A 3
A 4
A 4
A 4
I want output of the form:
A 2 3
A 3 1
A 4 2
What will be the most efficient query for this problem ?
select col1, col2, greatest(count(*) - 1, 1)
from your_table
group by col1, col2