id | value
1 | 0, 1
2 | 0,1,2
table_1
I have a table known as table_1 which have id and value column.
I would like to loop through the value column for each data which is seperated with comma.
Then, I would like to insert the every data into a new table.
As below:
id | value
1 | 0
1 | 1
2 | 0
2 | 1
2 | 2
table_2
This means that in table_2, it will have two rows of when user_id=1.
Example: user_id = 1, value = 0
user_id =1, value = 1
How to achieve this? Your response is very much appreciated.
In fact you want the opposite of GROUP_CONCAT() function.
I found this, that might work for you : http://thenoyes.com/littlenoise/?p=92
Hope that helps.
Related
Is there any constraint in MySQL in order to avoid from entering the same value in the same column or in another column?
For example, in the following table:
--------------------
| TABLE |
---------------------
| NUMBER1 | NUMBER2 |
---------------------
| 3 | 4 |
| 7 | 9 |
---------------------
I need to prevent the number 3 from being entered in any of the columns NUMBER1 or NUMBER2, since the value already exists in column NUMBER1; and that the number 9 cannot be ingested in either of the columns NUMBER1 or NUMBER2, since the value already exists in column NUMBER2.
In other words, I need that each value in each of the two columns be unique, through those two columns.
Thank you in advance.
Here is an indirect solution. You may refactor the design of your table such that it uses only a single column for both current columns. Then, introduce some sort of group columns to keep track of the relationships between the numbers from a given record. That is:
grp | pos | NUMBER
1 | 1 | 3
1 | 2 | 4
2 | 1 | 7
2 | 2 | 9
Now you only need a unique constraint on the single NUMBER column. Note that if you want to view your data as it appears now, a simple pivot query can handle that:
SELECT
grp,
MAX(CASE WHEN pos = 1 THEN NUMBER END) AS NUMBER1,
MAX(CASE WHEN pos = 2 THEN NUMBER END) AS NUMBER2
FROM newTable
GROUP BY
grp;
I have a MySQL table like below:
| ID | userIDs
---------------
| 1 | 4,3,5
| 2 | 2,3
| 3 | 1,2,3
I want to retrieve all the rows in which userIDs doesn't contain 1.
I tried
SELECT * FROM tablename WHERE 1 NOT IN (userIDs)
But it's not working.
Use FIND_IN_SET
SELECT * FROM tablename
WHERE find_in_set(1, userIDs) = 0
But actually you should rather change your table design. Never store multiple values in a single column!
I am trying to make a query in mysql to get any column which has a particular value for one specific row.
In Mysql we can get rows based upon any specific value of a column.
I have a table like :
+----+------------+------------+---------------+---------------+---------+----------------+---------
| ID | MSISDN | MissedCall | SponsoredCall | AdvanceCredit | ACvalue | SuitablePackId | AutoTimeStamp |
+----+------------+------------+---------------+---------------+---------+----------------+---------------------+
| 1 | 9944994488 | 1 | 0 | 1 | 0 | 1 | 2014-09-18 10:42:55 |
| 4 | 9879877897 | 0 | 1 | 0 | 0 | 2 | 2014-09-18 10:42:55 |
+----+------------+------------+---------------+---------------+---------+----------------+---------------------+
What i need is when i select a row based upon MSISDN , it should return all column names for that row whose value is fix (say 1).
So in above table for MSISDN = 9944994488 it should return
MissedCall
AdvanceCredit
SuitablePackId
What i have tried is :
SELECT COLUMN_NAME as names
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'bi'
AND TABLE_NAME = 'useranalysisresult'
This returns me column names of table.
But how to get column names with specific value.
Thanks for help in advance.
This is too long for a comment.
A SQL query returns a fixed set of columns. You cannot change the set depending on the row. You could do what you want using a prepared statement, although that would seem like an arcane approach.
You could return a single column with values concatenated together. Something like:
select concat_ws(',',
(case when MissedCall = 1 then 'Missed Call' end),
(case when SponsoredCall = 1 then 'Sponsored Call' end),
. . .
)
from useranalysisresult;
This would produce a list in a single column of the flags being set.
Try the below one. Here replace the YOUR_SCHEMA with your actual schema name and YOUR_TABLENAME with your actual table name:
select column_name from information_schema.columns
where YOUR_SCHEMA = //any condition
and table_name='YOUR_TABLENAME';
I have a database which contains product ID's and their rating out of 5 customers have given them. Each row in the database represents 1 review. Its quite simple and has the product ID and a 1 in the column where the review was 1, 2, 3, 4, or 5 star. It is laid out like this:
Product ID | 1 | 2 | 3 | 4 | 5 |
1294518 | 1 | 0 | 0 | 0 | 0 |
9226582 | 0 | 0 | 1 | 0 | 0 |
3946583 | 0 | 0 | 0 | 1 | 0 |
7392588 | 1 | 0 | 0 | 0 | 0 |
1196585 | 0 | 0 | 0 | 0 | 1 |
1196585 | 0 | 0 | 0 | 0 | 1 |
I want to merge the rows where I have duplicate product ID's. As per the example above there are two rows for product ID 1196585. In this case I would like to combine this into one row with a 2 under the 5 column to show this product has received two 5 star reviews.
To clarify I would like to modify the table in place so duplicate rows are combined, so that afterwards there are no duplicate product ID's and under each of the columns 1, 2, 3, 4 and 5 I am left with the number of times each of these product ID's has received a 1, 2, 3, 4 and 5 star review for example.
Please could you help me understand how this could be achieved with SQL?
This is a simple aggregation query:
select ProductId, sum(Rate_1) as Rate_1, sum(Rate_2) as Rate_3, sum(Rate_3) as Rate_3,
sum(Rate_4) as Rate_4, sum(Rate_5) as Rate_5
from t
group by ProductId
I changed the ostensible names of your columns to names using standard characters. 1 seems like a bad name for a column, for instance.
To modify the data in your table, you can use the following statement:
UPDATE product p
INNER JOIN (
SELECT "Product ID" as id,
SUM(product."1") AS s1,
SUM(product."2") AS s2,
SUM(product."3") AS s3,
SUM(product."4") AS s4,
SUM(product."5") AS s5
FROM product
GROUP BY id
HAVING count(*) > 1) sums
ON p.id = sums.id
SET p."1" = sums.s1, p."2" = sums.s2, p."3" = sums.s3, p."4" = sums.s4, p."5" = sums.s5
This will modify all your duplicated rows to be the same, with each column having the sum of the original rating counts. So all you have left to do is erase a bunch of duplicates, except for one. That has been asked a few times on StackOverflow, for example:
Remove duplicate rows in MySQL
How can I delete one of two perfectly identical rows?
My personal favorite is:
ALTER IGNORE TABLE product ADD UNIQUE INDEX ("Product ID")
I found a really easy way to do it was to export all the data into csv and open in excel and use the consolidate data function in excel then re-import back into MySQL. Not using SQL to make the amends but it was quicker and easier in the end.
May I suggest creating a view instead of combining the rows? That way you have your original data, in case you need it.
In a table I have the following value:
ID | Exercise1 | Exercise2 | Exercise3
1 | 0 | 0 | 0
2 | 0 | 0 | 0
When a user completes an exercise, the db switches from '0' to '1'. I'm looking for an sql query that searches by the ID number of the user returns the lowest column name that is set to 0.
EX:
ID | Exercise1 | Exercise2 | Exercise3
1 | 1 | 1 | 0
Here the query would return with exercise3, since exercise1 and exercise2 have previously been updated and completed by the user.
I found
SELECT COLUMN_NAME FROM information_schema.columns
but can't put it together with the sorting I'm looking for, any help would be deeply appreciated.
If you have only a handful of exercises (e.g. < 5), then you can simply hardcode the query with a series of nested IF() statements.
If you have more than that, then you should change your data model so each user/exercise mapping is stored in a separate row.
Something like this?
SELECT CASE
WHEN Exercise1=0 THEN 'Exercise1'
WHEN Exercise2=0 THEN 'Exercise2'
WHEN Exercise3=0 THEN 'Exercise3'
ELSE NULL
END AS Exercise
FROM MyTable
WHERE ID = SomeID
Hmmm... you have problems because your design is wrong. The problem is that your database design was affected by how you imagine the presentation of the table. But the database thinking is different. The database would be normally designed this way:
StudentID | ExcerciseID | Completed
1 | 1 | 1
1 | 2 | 1
1 | 3 | 0
2 | 1 | 0
....
And then you can do:
select StudentID, min(ExcerciseID) as FirstExcerciseNotCompleted
from Excercises
where Completed = 0
to see first incomplete excercise for each student, or if you want set next completed excercise to Student 1, just do:
update Excercises
set Completed = 1
where Student = 1 and ExcerciseID = (select min(ExcerciseID) from Excercises where StudentID = 1 and Completed = 0)