This question already has an answer here:
Query Distinct values from a multi-valued column
(1 answer)
Closed 5 years ago.
i'm developing a quiz website. In my database, I need a table which shows
reported quiz errors. It should look like this:
______________________________________________________________________
|key| quiz_number | who_reported_this_error | reported_number |
-----------------------------------------------------------------------
| 1 | 5 | goid482,saiai10,hahakaka | 3 |
-----------------------------------------------------------------------
| 2 | 3 | fiiai55,kihogi84 | 1 |
-----------------------------------------------------------------------
If a user named hanabi reported an error about quiz number 5,
first I need to check the who_reported_this_error column because
I don't want for a user to report same error twice. If the user 'hanabi' doesn't exist in "who_reported_this_error" column I should update row 1.
Now for my problem. I want to find a row which I should update with a key column, and the key column's number should automatically increased. But I know that I can't use a WHERE clause on this primary key. Hhow can I solve this problem?
The problem is with the table schema. NEVER store comma-separated data in a single column. You should structure the table to look more like this:
____________________________________________
|key| quiz_number | who_reported_this_error |
────────────────────────────────────────────
| 1 | 5 | goid482 |
---------------------------------------------
| 2 | 3 | fiiai55 |
---------------------------------------------
| 3 | 5 | saiai10 |
---------------------------------------------
| 4 | 5 | hahakaka |
---------------------------------------------
| 5 | 3 | kihogi84 |
--------------------------------------------
You might also want a timestamp column on this table. Then, put a UNIQUE constraint on the quiz_number and who_reported_this_error columns to prevent the duplicates.
If you later need to see everyone who reported errors for quiz 5 in the same record, use MySql's group_concat() function to build that information on the fly. Just don't store the data that way.
The key column has nothing to do with this question. You certainly can use your primary key in a WHERE clause. It just won't help you in this case because that data isn't relevant to the problem at hand.
Related
I've been struggling with this for a while but with no success.
I have two tables that might have a relation but not necessarily.
FIRST
+----+-----------+------------+
| ID | LPR | TIMESTAMP |
+----+-----------+------------+
| 1 | QWE123RTY | 05-05-2020 |
+----+-----------+------------+
| 2 | ZXC789IOP | 05-05-2020 |
+----+-----------+------------+
| 3 | ASD567FGH | 05-05-2020 |
+----+-----------+------------+
SECOND
+----+-----------+------------+----------+
| ID | LPR | TIMESTAMP | OWNER_ID |
+----+-----------+------------+----------+
| 1 | AAA111BBB | 04-05-2020 | 3 |
+----+-----------+------------+----------+
| 2 | QWE123RTY | 02-05-2020 | 1 |
+----+-----------+------------+----------+
| 3 | BBB222CCC | 14-05-2020 | 1 |
+----+-----------+------------+----------+
I basically want to replicate SELECT * FROM FIRST JOIN SECOND WHERE LPR="QWE123RTY" in prisma but to no avail. I cannot use a foreign key (at least to my knowledge) since the foreign key in SECOND might not always be present in FIRST as vice-versa.
An alternative that I think might work is to run two separate queries where I retrieve the matching records in SECOND and then run
prisma.FIRST.findMany({
where: {
LPR: { in: ['QWE123RTY', 'BBB222CCC'] }
}
})
Has anyone actually managed to do something like that?
I have found a solution to my problem, by using prisma.$queryRaw which I actually didn't know it existed for the few months I've been using it and only stumbled upon it now.
Here's the documentation link for reference: Prisma - Raw database access
The end solution was:
await prisma.$queryRaw`SELECT * FROM FIRST JOIN SECOND ON FIRST.LPR=SECOND.LPR`
P.S. The only issue I ran into by using the queryRaw method instead of the regular Prisma Client was that the BLOB type values are retrieved as strings, instead of as a Buffer, but that can be handled both on the front-end as well as in the back-end accordingly with minor modifications to the response.
When a row is duplicated in our system, it inserts the new row with a reference back to the ID of the one it was duplicated from.
If that new row is then duplicated, it has a reference back to the row it was duplicated from and so on.
What I cant work out is how to follow this trail in a SELECT.
Take the following data...
+------+-------------------+
| ID | duplicated_from |
+------+-------------------+
| 1 | NULL |
| 2 | 1 |
| 3 | 2 |
| 4 | NULL |
+------+-------------------+
So, given ID 1, how would you look up all the slides in the chain that have been duplicated off it?
Or is this something that will have to be done at an application level?
Seems that you are after a recursive query, i found this solutions that may help you How to create a MySQL hierarchical recursive query
HTH
I have a poorly created table I want to update. It is set up as
ID
Name
Value
Because a given Name can have more than one value right now the Value field is varchar and populated with comma delimited values:
12,15,92
I would like to create an update or create table query that will make those into separate records so a table with
ID | Name | Value
1 | Bob | 5,6,9
2 | Alice| 5,9
3 | Ted | 1
ends up as
1 | Bob | 5
2 | Bob | 6
3 | Bob | 9
4 | Alice | 5
5 | Alice | 9
6 | Ted | 1
In searching online it appear this is a pretty common issue and I found one of several functions for splitting delimited fields into records here:
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows
This is an old question but there is a mysql function for this, it name is group_concat, you can see the function in the offical documentation, also you can see an example here
So, I am working in Visual Studio 2013. And I have a table like this:
id | fk | data
----------------------
1 | 1 | something1
2 | 1 | something2
3 | 1 | something3
. | . | ...
. | . | ...
6 | 2 | ...
The fk is a foreign key to another table. I want to select all the rows which have the same fk. There is an undefined number of the same fk entries (there could be 5 rows with value 1 and 3 rows with value 2, etc.) But, I want to be able to switch said fk in my program so that when i put the DB in a Form in Visual Studio, clicking a button next would get me the next value of the fk. For example, I would like the first result to come out like this:
id | fk | data
----------------------
1 | 1 | something1
2 | 1 | something2
3 | 1 | something3
And when I click next to get me to the next entry, the result would be:
id | fk | data
----------------------
6 | 2 | ....
7 | 2 | ....
Is there a way in SQL to combine just the results with the same fk value? All of the solutions I have found so far gave me all of the fk values (all entries). I haven't found a way to get just all the entries with a single, unique value in the fk column.
You're looking for a basic SELECT statement with a WHERE clause:
SELECT
id, fk, data
FROM
your_table_name
WHERE
fk = 1
I can't help you with the VisualStudio stuff though, but you'll just have to repeat the same query incrementing the fk value in the WHERE clause
Imagine the following (very bad) table design in MSSQL2008R2:
Table "Posts":
| Id (PK, int) | DatasourceId (PK, int) | QuotedPostIds (nvarchar(255)) | [...]
| 1 | 1 | | [...]
| 2 | 1 | 1 | [...]
| 2 | 2 | 1 | [...]
[...]
| 102322 | 2 | 123;45345;4356;76757 | [...]
So, the column QuotedPostIds contains a semicolon-separated list of self-referencing PostIds (Kids, don't do that at home!). Since this design is ugly as a hell, I'd like to extract the values from the QuotedPostIds table to a new n:m relationship table like this:
Desired new table "QuotedPosts":
| QuotingPostId (int) | QuotedPostId (int) | DatasourceId (int) |
| 2 | 1 | 1 |
| 2 | 1 | 2 |
[...]
| 102322 | 123 | 2 |
| 102322 | 45345 | 2 |
| 102322 | 4356 | 2 |
| 102322 | 76757 | 2 |
The primary key for this table could either be a combination of QuotingPostId, QuotedPostId and DatasourceID or an additional artificial key generated by the database.
It is worth noticing that the current Posts table contains about 6,300,000 rows but only about 285,000 of those have a value set in the QuotedPostIds column. Therefore, it might be a good idea to pre-filter those rows. In any case, I'd like to perform the normalization using internal MSSQL functionality only, if possible.
I already read other posts regarding this topic which mostly dealt with split functions but neither could I find out how exactly to create the new table and also copying the appropriate value from the Datasource column, nor how to filter the rows to touch accordingly.
Thank you!
€dit: I thought it through and finally solved the problem using an external C# program instead of internal MSSQL functionality. Since it seems that it could have been done using Mikael Eriksson's suggestion, I will mark his post as an answer.
From comments you say you have a string split function that you you don't know how to use with a table.
The answer is to use cross apply something like this.
select P.Id,
S.Value
from Posts as P
cross apply dbo.Split(';', P.QuotedPostIds) as S