Eloquent laravel : how count data from json column [closed] - mysql

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
Hi this is Products table
id | name | price
22 | product_1 | 10.00
33 | product_2 | 10.00
44 | product_3 | 10.00
and this Orders table.
in cartproducts column i have use json.
id| cartproducts | summ
1 | [{"productid":22,"quantity":1,"options":"[]"}] | 10.00
2 | [{"productid":33,"quantity":1,"options":"[]"},{"productid":44,"quantity":2,"options":"[]"}] | 30.00
3 | [{"productid":22,"quantity":3,"options":"[]"}] | 30.00
Using eloquent laravel how can i count how much order i have for each productid in Orders table using groupby method or other methods.
this is a simple imagine
$productorders = \App\Orders::where('productid',$productid)->groupBy('??')->orderby('count', 'desc')->get();
Desired output ( i will use my output in table sorted by Orders count )
product name | orders count | total earning | total quantity
product_1 | 2 | 40 | 4
product_2 | 1 | 10 | 1
product_3 | 1 | 20 | 2
thanks

You are making it hard for joining, and this table (Orders) as I can see it's only useful for logs. You can't join any other table to this table. Because you need to extract the corresponding values (ProductId and quantity) from the JSON field.
But even if you extract those fields, you can't handle the second record in Orders table, since you'll have two different productID, so you need to have an array field to extract the productId's into it, but then what you want to do with the array field, You can't really join it, and even if it's possible it'll make too way hard. for such small things.
I'll suggest stopping storing data which they eventually needs to be used as a joining key to other tables. You can store each item in OrderTable separately without using JSON.
There is a second solution which it's obvious, using PHP to handle the process in the application layer. You can json_deocde the contents of the orders and then the rest of the story which you know it.

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".

How to create a query to check which sub-String (from DB) is contained in a given String? [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 3 years ago.
Improve this question
I am trying to write a query, which will compare the country phone code from one table with the full phone number from another table and will output the appropriate country name.
I was playing playing a bit with the conditional query, but I don't know how to bite it.
My DB contains two tables:
tblCall
CallID | Caller |
____________________________
1 | +4411111111 |
2 | +4911111111 |
tblCode
CodeID | Code | Country |
_____________________________________
1 | +44 | UK |
2 | +49 | Germany |
So I need a query which will check if the code is included in the phone number and will print out the appropriate country name, like this:
CallID | Caller | Country
______________________________________
1 | +4411111111 | UK
2 | +4911111111 | Germany
You would use a join. If the code is always three characters, you can use a comparison such as:
select ca.*, co.country
from tblCall ca left join
tblCountry co
on left(ca.caller, 3) = co.code;
If it is variable, then like:
select ca.*, co.country
from tblCall ca left join
tblCountry co
on ca.caller like concat(co.code, '%')

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).

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.

take data from mysql stored with "-" [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 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