take data from mysql stored with "-" [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My database is like this:
users table
_id_____favorites_____
1 | -53-87-96 |
2 | -12-54-87 |
images table
_id_____url____________
1 | smile.jpg |
2 | lol.jpg |
I stored favorites with seperator "-"; these favorites numbers are images id. When a user logs in they want to see their favorites. How can i write this query?

Favorites should be retrieved via a one-to-many relationship. The favorites table should look like
id | favorite
---+---------
1 | 53
1 | 87
1 | 96
2 | 12
2 | 54
2 | 87
Then you
select all from favorites where id = 1
Or whatever the id is.

Never, never, never store multiple values in one column!
That will give you serious problems, like in your case. You should change your database structure. You could add a favorite table
favorites table
+--------+------------+
|user_id |favorite_id |
| 1 | 53 |
| 1 | 87 |
| 1 | 96 |
+--------+------------+

Your current schema design is bad, you should properly normalize it into 3-table design.
User Table
UserID (PK)
UserName
other columns...
Images Table
ImageID (PK)
ImageLink
other columns...
Favorites Table
UserID
ImageID
other columns...
Anyway, to answer your question, MySQL's FIND_IN_SET will help but you need to replace - with , using REPLACE().
SELECT a.ID,
b.url
FROM users a
INNER JOIN images b
ON FIND_IN_SET(b.id, REPLACE(a.favorites, '-', ',')) > 0
WHERE a.ID = 1
SQLFiddle Demo

Related

Is it worth storing integer as string in table because it nice fits with business logic? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
In my application there are two types of users, anonymous and authenticated.
Authenticated users has integer for id, anonymous users has uuid as id. There is a table Item
which stores items that user has.
I have two ideas about how to store different id's in one table
First create column user_id as string and store both users ids in one column:
Item
+---------+----------------------------------------+----------+
| item_id | user_id | quantity |
+---------+----------------------------------------+----------+
| 1 | '1' | 2 |
| 2 | 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' | 3 |
| 3 | '2' | 1 |
+---------+----------------------------------------+----------+
Second create two different columns user_id (integer) and anon_user_uuid (uuid),
Item
+---------+---------+----------------------------------------+----------+
| item_id | user_id | anon_user_uuid | quantity |
+---------+---------+----------------------------------------+----------+
| 1 | 1 | null | 2 |
| 2 | null | 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' | 3 |
| 3 | 2 | null | 1 |
+---------+---------+----------------------------------------+----------+
In first case I need to write four queries for retrieving, updating, creating, deleting items.
In second I need to write eight queries for crud operations.
So my question, is it okey to have design as in first case (first table above)?
It looks like you've got two completely different types of values. In that case two columns is appropriate.
The reason you'd have them as one column is so you can set up foreign keys or other relations, but that's not possible if some of these can't be matched like that.
For consistency you might want to give every user an "anonymous" ID and just use that internally. Some of these might map to a registered user, others may not.
It's also possible to just create a user record for anonymous users and if the user registers after the fact, just populate the other fields and change that status to "registered".

MySQL 2 tables combine with foreign key - See entries with specific function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 2 tables - the first table has an ID with name and function. The second table has a date and the ID from the first table.
Table 1:
ID | name | func1 | func2
1 | stef | 1 | 1
2 | rob | 0 | 1
3 | bob | 0 | 0
4 | isa | 1 | 0
Table 2:
date | ID
2020-05-01 | 2
2020-05-01 | 4
2020-05-01 | 3
Now I need these IDs from table 2, which have a 1 at "func1". So in this case the ID 1 and 4.
How do I get these?
I tried a lot with MySQL, but didn't find a solution. Only errors.
Thanks and best regards
The query needs a join, as shown below:
select b.ID
from table1 a
join table2 b on b.ID = a.ID
where a.func1 = 1
However this query will find only 4 and not 1, since the latter does not exist in Table 1.

How to structure database for Tinder functionality [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm building a functionality similar to Tinder. People can 'like' or 'skip' photo's of someone else, if both people 'like' each other then there is a match.
What is the best approach of a database structure for this functionality? I want to be able to get a list of all matches and all matches per Person.
Approach 1:
Person | JudgedPerson | Like
------ | ------------ | ----
1 | 2 | yes
2 | 1 | yes
1 | 3 | yes
3 | 1 | no
2 | 3 | yes
This looks like a logical approach, but it is difficult to create a MySql query to discover matches. Or is there a simple way to discover it?
Approach 2
Person1 | Person2 | P1LikesP2 | P2LikesP1
------- | ------- | --------- | ---------
1 | 2 | yes | yes
1 | 3 | yes | no
2 | 3 | yes | null
It's easy to create queries to get matches, but the datamodel might be not the best.
What is the best approach?
If approach 1 is the best approach, what mysql queries can I use to discover the matches?
I don't have a formal reason for why I prefer the first option, but it is clear that the second option is not completely normalized.
To query the first table and find pairs of people who like each other, you can try the following self join:
SELECT DISTINCT LEAST(t1.Person, t1.JudgedPerson) AS Person1,
GREATEST(t1.Person, t1.JudgedPerson) AS Person2
FROM yourTable t1
INNER JOIN yourTable t2
ON t1.JudgedPerson = t2.Person AND
t1.Person = t2.JudgedPerson
WHERE t1.Like = 'yes' AND
t2.Like = 'yes'
Note: I added DISTINCT along with LEAST/GREATEST to the SELECT clause because each match will actually come in the form of a duplicate. The reason for this is that, e.g. 1 -> 2, 2 -> 1 would be one matching record, but also 2 -> 1, 1 -> 2 would also be a second record.
Personally, I would consider adding another option to the presented ones: having 2 tables - likes and matches:
Matches
Person1 | Person2
------ | --------
1 | 2
1 | 3
2 | 1
3 | 1
Likes
Who | Whom | Likes
--- | -----|---------
2 | 3 | 'no'
Getting matches would be a simple query:
SELECT p.*
FROM Persons p
INNER JOIN Matches m ON p.Id = m.Person2
WHERE m.Person1 = #judgedPersonId
The idea is to precompute matches instead of resolving them on each query (either in background process or during Like operation - to remove two-way likes and add records to matches tables).
This way one gets faster and easier queries when selecting matches, but the approach involves additional complexity computing "matches" and doing related queries (e.g. finding people who are not yet matched and not disliked).

How to combine two tables into one with a new field with table source? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
table one
+-----+--------+--------+
|date |index |idOne |
|-----+--------|--------+
| | | |
table two
+-----+--------+--------+
|date | index | idTwo |
|-----+--------|--------+
| | | |
query result where index = "5"
+---------+--------+--------+----------+
|date | index | id | idType |
|---------+--------|--------+----------+
|10/10/91 | 5 | 1 | One |
|10/11/91 | 5 | 2 | One |
|10/12/91 | 5 | 1 | Two |
These are related questions but not the same:
This one has no different fields
I want to combine/union two tables and have it create a field that identifies which table it came from
This one has no table distinction field
Combine two tables that have no common fields
You can use the union all operator to join the two queries and add the idType column by selecting a literal:
(SELECT `date`, `index`, idOne AS id, 'One' AS idType
FROM one
WHERE id = 5)
UNION ALL
(SELECT `date`, `index`, idTwo AS id, 'Two' AS idType
FROM two
WHERE id = 5)

sql left join with subselection in comma-delimited field value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to take the column Ngr from table groups WHERE the marks of the students are bad(=2) in more than two subjects and the number of these students are the half from the group:
SELECT Ngr
FROM subject
LEFT JOIN groups ON (subject.Ngr = groups.Ngr)
WHERE....
and here I can't continue
thanks for your future advice!
groups:
| Ngr | Name_of_speciality |
----------------------------
| 1 | Physics |
..........................
subject:
| Ngr | Name | marks |
-------------------------------------------
| 1 | Physics|2,2,3,3,5,5,5,4,3,2,2,5,2,4|
...........................................
Good luck!
Continuing from the comment;
you would have to use LIKE; but wouldn't it be better to redesign your tables to have each mark be a seperate row?
#Luceos, how can I do that?
it depends on your table format; you duplicate Physics in your table, why's that? You have a group called Physics and a subject called Physics, should that be an exam name like Physics 1? Then if you dont want to connect marks to students you can make an auto increment column for primary id:
| id | Ngr | Name | mark |
-------------------------------------------
| 1 | 1 | Physics|2 |
-------------------------------------------
| 2 | 1 | Physics|5 |
...........................................
Etcetera.. This way you can select by mark = 2 or mark <= 2 and get results back in the join.