I have list of images in a table (create table id, name, sequence). I want to arrange objects in a certain sequence.
e.g.
1, "rose", 1
2, "jasmine", 2
3, "lilly", 3
If I move "lilly" in front of "rose", the sequence would be as follows
1, "rose", 2
2, "jasmine", 3
3, "lilly", 1
Is there a way to automatically achieve this via a gem, since I don't want to update all sequence values in the table by writing update code myself.
You can use the ActsAsList gem, which does the position updating work for you when you change the position of a record.
Related
I'm trying to update my column because I'm working on a "Kick from Team" function. I've tried different solutions that I've found on google.
My first attempt was this:
UPDATE table SET aMembers = JSON_REMOVE(aMembers, '$[1]') WHERE id = 1
aMembers looks like (Column type: JSON) :
[1, 2, 8, 99, 12, 233, 819]
That works somewhat. It'll remove the given index from aMembers. This is not what I'm after tho. I'm after something that'll remove value 1 from aMembers.
Alright, then I tried this one:
UPDATE table SET aMembers = JSON_REMOVE(aMembers, JSON_UNQUOTE(JSON_SEARCH(aMembers, 'one', '1'))) WHERE id = 1
This sets my whole column as NULL which is also not what I'm looking for. Am I doing this wrong or is this just not possible? Is there a query that'll remove id 1 from my column or am I forced to
1. With js - Get column aMembers
2. Find out at which index ID 1 is at
3. Create a new query that'll remove index X
For DB I am using MariaDB.
For my frontend I am using NextJS.
Backend is NodeJS.
I chose to solve this by using index. Each time I print the members out they will be printed in the same positions as in the JSON column which means that I can simply use index to remove x member from the column.
So each child, when printed out, get's an Index prop and this prop is being set to my api that kicks them. This then updates the JSON in mysql table row to remove index x from the column.
I have a database with 2 tables that look like this:
content
id name
1 Cool Stuff
2 Even Better stuff
--
contentFields
id content label value
5 1 Rating Spectacular
6 1 Info Top Notch
7 2 Rating Poor
As you can see the content column of the contentFields table coincides with the id column of the content table.
I want to write a query that grabs all of the content and stores the applicable content fields with the right content, so that it comes out to this:
[
{
id: 1,
name: 'Cool Stuff',
contentFields: [
{label: 'Rating', value: 'Spectacular'},
{label: 'Info', value: 'Top Notch'}
]
},
{
id: 2,
name: 'Even Better Stuff',
contentFields: [
{label: 'Rating', value: 'Poor'}
]
}
]
I tried an inner join like this:
SELECT * FROM content INNER JOIN contentFields ON content.id = contentFields.content GROUP BY content.id
But that didn't do it.
*Note: I know that I could do this with 2 seperate queries, but I want to find out how to do it in one as that will dramatically improve performance.
What you are trying to achieve is not directly possible with SQL only.
As you have already stated yourself, you are looking for a table within a table. But MySQL does not know about such concepts, and as far as I know, other databases also don't. A result set is always like a table; every row of the result set has the same structure.
So either you let your GROUP BY content.id in place; then, for every row in the result set, MySQL will select a random row from the joined table which fits to that row's content.id (you even can't rely on that it is the same row every time).
Or you remove the GROUP BY; then you will get every row from the joined table, but that is not what you want as well.
When performance is an issue, I would probably choose the second option, adding ORDER BY content.id, and generate the JSON myself. You could do so by looping through the result set and begin a new JSON block every time the content.id changes.
Disclaimer The following is pure speculation.
I don't know anything about node.js and how it transforms result sets into JSON. But I strongly assume that you can configure its behavior; otherwise, it actually would not be of any use in most cases. So there must be a method to tell it how it should group the rows from a result set.
If I am right, you would first have to tell node.js how to group the result set and then let it process the rows from the second option above (i.e. without the GROUP BY).
I have to display a product on magento 1.9 which has 5 distinct attributes that determine price, namely:
1, Size (9,14,18)
2, Metal (Gold, Silver)
3, Gender (Women, Men)
4, Stone (Diamond, Swarovsky)
I have made the attribute(configurable) for each and added to attribute set. Each one of these would alter the price based on drop down selection.
How can i make it work making it dynamic price such as the price will change dynamically based on each drop down selection. for example:
Blockquote
1, Size->Metal->Gender->Stone
2, 9->Gold->Set->Diamond = $5000
3, 9->Gold->Set->Swarovsky = $3000
4, 9->Silver->Women->Diamond = $4500
Blockquote
So we have multiple options here for price selection. Is this something magento can handle or only option is to use a third-part extension.
Do you know any free third-party extension for this or any way it can be coded to upload xml,csv files of these combination allowing the price to change every time the user selects different option.
Thanks.
This could by an x/y problem, so if there's a better approach altogether, I'd love to hear it. The summary of the problem starts at the last code block, so skip to that if you want and come back to the details if needed.
I am building a content manager (if nothing else, for the experience). To get started understanding what data I need, I made a "pages" table with this structure:
id (page id) | path (where it is found) | title | content | (etc.. some other stuff)
So, it is the content area that is trouble. Let me explain the end result: I need a content map that has an object of positions, each which are arrays of content that belongs in that position. Here's a sample:
{ header: [5], main: [4,1], footer: [2,8,9] }
Those id's will then go to a content table, pull each item (like id 5) and replace the id with the actual content/settings for that item. That isn't really as relevant for now.
I can't just store the json right to the db in the content field of "pages" because if I were to delete content item "5", it would still be in the json for that page. I need to be able to delete content item 5, and it automatically be removed from wherever it is used.
That lead me to this:
I create another table that tracks where content items are used and the order. Here's the structure for that table (content_locations):
pageId (what page this content is on) | contentId (which content) | position | order
So, I think that gets me on the right track on being able to delete things... if I delete a page, I believe I can set it up to delete the rows it has in content_locations and also set it up so that removing a content item will remove the content_locations rows for that item. I honestly haven't tried that yet, but I'm pretty sure that's possible. If not, I'm really lost :)
My main issue seems to be the ordering. Consider this set of data:
pageId, contentId, position, order
2, 6, header, 0
2, 1, header, 1
2, 4, header, 2
How could I insert a first item (can't insert before 0) or what if I wanted to insert in between one of those (1 1/2) or what if I deleted item 1? I run into a big problem with reordering. Is this a problem with my idea of how to structure the data, or is there a good solution for dealing with an ordering column such as that?
Other issues notwithstanding, I'll just comment on reordering...
If you need to make space to insert a new row, you can easily move the other rows out of the way by:
UPDATE your_table
SET order = order + 1
WHERE pageId = ... AND order > 0
(Replace 0 with the actual position at which you want to insert the new row.)
You can do the opposite after delete, or you can just leave a hole - these holes can be easily "collapsed" at the presentation level.
Unless you have a large number of rows per page, this should be reasonably quick. If not, consider leaving holes in advance, and moving elements only if the hole is completely filled.
BTW, to switch two rows, you can do something like this:
UPDATE your_table
SET order =
CASE order
WHEN 2 THEN 3
WHEN 3 THEN 2
END
WHERE pageId = ... AND order IN (2, 3)
(Replace 2 and 3 with actual positions.)
I think the correct way to do this is to have an after column that references the pageId to say that this is after that. Although that's going to make for some complex SQL. Alternatively just start the order off as 0, 1000, 2000, which gives space to insert things.
One way to do the insert is to multiply every order value by two and add one -- this is a fairly trivial query. Then you have space to insert, so your table becomes:
pageId, contentId, position, order
2, 6, header, 1
2, 1, header, 3
2, 4, header, 5
and then you just insert the order as (order * 2) and it's guaranteed to have a space.
Periodically you'll have to collapse the numbering in order to stop overflowing the values -- but if you check if there's a space beforehand then this should be rare. (You can use ROW_NUMBER to do the renumbering).
I wouldn't embed JSON in the database at all if you can avoid it, parse it and add it to separate tables to make your life easier later.
I am asked to allow users to input multiple values in EVERY field. So the option is limitless.
For example. Columns are:
CompanyID-
Company name
Website
Key_Markets
M&A_History
Highlights
Region
Comments
A scenario is a company can have multiple websites,key markets, region, etch. How would I do this professionally? I am thinking of putting every column a seperate table.
Basically there are three ways to realize this.
1) Write multiple fields into one column seperately. This would be a very bad design and you would have to handle the splitting in your application - Do not do that ;-)
2) Use one table with multiple groups to store the data. This would make sense for parameters but not really if you have different values for each customer. For example:
CompanyID
GroupID
Position
Value
Example:
108001, 'homepage', 1, 'www.mypage.com';
108001, 'homepage', 2, 'www.mysecondpage.com';
108001, 'homepage', 3, 'www.anotherpage.com';
108001, 'markets', 1, 'erp';
108001, 'markets', 2, 'software';
108001, 'region', 1, 'germany';
108001, 'region', 2, 'austria';
108001, 'region', 3, 'poland';
3) Use seperate tables for each 1:n relation! This would be the best solution for your needs I guess. This would have the advantage that you can easily extend your schema and store more data in it. For example if you decide to store the amount of users for each region or key markets etc.
Another point: Use n:m relations to avoid double content in your database! For example should the key-markets and regions be stored in a completely seperated table and you store the IDs of the customer and the key-market in a crosstab. So you do not need to store the key-markets as a string for each customer!
You would need a database structure like:
table_master_companies
- record_id
- company_name
table_websites
- record_id
- company_id
- website_address
table_key_markets
- record_id
- company_id
- key_market
etc. You would then need to use joins to concat all the information into a single recordset.