Create Multi-Object JSON Arrays in MySQL - mysql

As the image suggests I need to find a way to put the values from another table with same ID into an JSON object consists of JSON-array.
This is the article click for the link I am seeing and it's in SQL Server I need to use it in MySQL.
Any help will be appreciated.
For now, I am just doing inner join within two tables.
But not getting result as expected.

Related

Is there any way to read two different names from same column?

I have table and in the images column I am saving the names of the images needed for that row data, I want to know if there is any way to read multiple image name which are in the same column(and row) and fetch corresponding image from the folder.
I can read one image name and fetch that image from the folder, but not more than one.
Do I have to create multiple column to achieve this?
Even of you could, its not a good design to store multiple image names on the same column(not just for images, but any type of data in general), research about "normal forms"(or normalization) in database design, the best approach would be to store them on another table with a 1 to many relationship(the images table would have a foreign key to the original row),this way you can easily fetch image by image with a simple join ,and use the current code "for one image" you already have
Microsoft SQL 2016 have JSON storage support. You could store the names in JSON in a single column and return the result as JSON. Something like:
SELECT a.ImageNames FROM table1 a FOR JSON
Then you could load the JSON into an object with a list property.
Some variation of that. It's difficult to tell because we don't know what language or framework you're using other than MS SQL. But if you want to store multiple values in a single column/row, JSON would be a good way to do it.
You could also just use a simple CSV value.

Need help starting simple MySQL database using data from Excel

I'm and intern and I've been tasked with something I'm pretty unfamiliar with. My manager has requested I create a simple MySQL database using data from an Excel file(s) and I have no idea where to start. I would normally ask someone here for help but everyone seems to be really busy. Basically, the purpose of the database is to see what different object-groups relate to one another so as to keep things standardized. Trying not to go into detail about things not really relevant.
I was asked to first design a schema for the database and then I would get an update on how to implement it. Would I just start by writing queries to create tables? I'm assuming I would need to convert the Excel files to .csv, how do I read this data and send it to the correct table based on Object Type (an attribute of each object, represented in a column)?
I don't want to ask too much right now, but if someone could help me understand what I need to do to get started I would really appreciate it.
Look at the column headers in your spread sheet.
Decide which columns relate to Objects and which columns relate to Groups
The columns that relate to just Objects will become your field names for the Object table. Give this table an ID field so you can uniquely identify each Object.
The columns that relate to the Groups will become field names for a Group table. Give this table an ID field so you can uniquely identify each Group.
Think about if an Object can be in more than one Group - if so you will probably need an Object-Group table. This table would most likely contain an ObjectID and a GroupID.

Delete sql rows with multiple values within where IDs do not have a match in another table

I have two table:
Table: Options
Options
Id xItems
- ItemA,ItemB,ItemC,etc
Table: Items
Items
Id
-
I am attempting to delete all Items rows that are not listed within Options.xitems
I attempted to execute the SQL statement
DELETE FROM items
Where items.id NOT IN (SELECT xitems FROM options)
However the problem is that multiple values are contained within XItems and I only managed to delete rows where Item.Id was the first or only value.
Would appreciate any kind help
EDIT: The following update added from the OP's post as an Answer.
The server is MySQL(tags edited accordingly) which allows one to enter an SQL statement below to execute against any database table or tables. I am a front end dev and get confused with this stuff.
John, I ran the code you posted. Here is the acutal code I applying against backedup test tables
DELETE FROM xbak514q_ecom_prodoptionsel
WHERE NOT FIND_IN_SET(xbak514q_ecom_prodoptionsel.id, (SELECT xprodoptionsel FROM xbak514q_ecom_prodoptions))
which returned the following error:
A problem was encountered while executing the SQL statement submitted.
The error was reported as: The MySQL extension encountered a problem
submitting an SQL statement. MySQL reported the error as: Subquery
returns more than 1 row
This database was configured by a software company who set up an e-comm site. The Items, Product options and selection items(add ons) are quite extensive. Should I consider reformatting the tables?
Again thanks for your kind help
have you tried using find_in_set()??
DELETE FROM items
WHERE NOT FIND_IN_SET(items.id, (SELECT xitems FROM options))
FIDDLE DEMO
NOTE:
find_in_set() is only for MySQL but since you have it tagged for both this may or may not be the solution. however the function looks for a comma separated list that is a single string or item and takes the first argument as the search string
RECOMMENDATION.
you should NEVER store data in the database as a comma separated list like that.. it causes HUGE issues in the future. please consider normalizing your database. if you want a way to do that just post a comment and I'll write up a query that will normalize it for you.

SQL Inner joining a column from two tables with a single user id

I've been trying to get this SQL query running for a while now and can't seem to get the last little bit going.
The backend database to all this data is a Drupal install with data spread out across a number of modules, so I need to do a lot of joining to get a certain view table set up that I need for a third-party application.
It's hard to explain the entire schema, but here's the sqlfiddle:
http://sqlfiddle.com/#!2/68df0/2/0
So basically, I have a userid which I map to a profile id through a join. Then I need to use that profile ID to pull the related data about that profile from two other tables. (there should only be one row with each pid in each of the other tables)
So in the end, I would like to see a table with username, othername, and key_id.
I got the first two pieces in there, but just can't seem to figure out how to join in the othername, and keep getting null.
The server is running MySQL.
Thanks guys!
LEFT JOIN other_name
ON profile_link.pid=other_name.pid;

Referencing different table based on field value

I'm trying to design some tables and wondering if I'm missing the correct way to do it. For example, I have a 'main' table (would hold any top level generic info) and I have a field in that table 'type'. Right now I have a different table for each 'type' so I'm using if statements to grab the correct info.
So select * from Main -> then using that type field value to determine which table I need to get my more detailed information from. If type is 1 query TableA... if type is 2 query TableB... etc...
Right now this is working... there is a fixed amount of 'types' but I'm pretty helpless on any nice JOIN statements in this scenario.
Does this make sense? Just seems like I'm doing this a really messed up way.... Thanks for any help or suggestions.
Depending on the language you re using, you could generate your SQL dynamically, placing the 'type' table into the dynamic SQL and doing JOINs that way. We do something like this in an application that uses C++, C#, PHP and SQL Server stored procedures, and dynamic SQL is our solution.