Retrieve all records in MySQL that have the same value in this particular field

Explaining my MySQL issue in words is tough for me as a beginner, and finding solutions is proving really challenging.

This problem is much clearer when seen visually:

I am trying to select exchange_pair_id's that have the same pair_id to form an array.

https://i.sstatic.net/NKG02.png

So based on the data above, my MySQL query would result in an object:

{ pair_id: 1, exchange_pair_id: [183, 1] }

I understand this might not be the most clear question, but I'm struggling to find the right search terms to solve it.

Updated for clarification/brevity:

+------------------+-------------+---------+
| exchange_pair_id | exchange_id | pair_id |
+------------------+-------------+---------+
| 1                | 3           | 1       |
+------------------+-------------+---------+
| 183              | 1           | 1       |
+------------------+-------------+---------+
| 69               | 2           | 2       |
+------------------+-------------+---------+
| 12               | 4           | 2       |
+------------------+-------------+---------+
| 2                | 3           | 2       |
+------------------+-------------+---------+
| 3                | 3           | 3       |
+------------------+-------------+---------+

Desired outcome from a JavaScript MySQL select query:

[
 { pair_id: 1, exchange_pair_id: [1, 183] },
 { pair_id: 2, exchange_pair_id: [69, 12, 2] },
 { pair_id: 3, exchange_pair_id: [3] }
]

Answer №1

Here is a query that I have in mind, but I am looking forward to hearing your thoughts in the comments section. Essentially, by using GROUP BY, you can retrieve values for each pair_id in separate columns:

SELECT pair_id, MIN(exhange_pair_id) AS id1, MAX(exchange_pair_id) AS id2
FROM yourtable
GROUP BY pair_id;

Updated version: Would you be willing to give this a try with your own data? With MYSQL, you are able to concatenate fields using a separator (,)

SELECT pair_id, GROUP_CONCAT(exhange_pair_id) AS exhange_pair_id 
FROM yourtable
GROUP BY pair_id

Answer №2

Execute

select exchange_pair_id from sampletable where pair_id=1
and it will output the array [1,183]

Answer №3

Here is the SQL query that you should use:

SELECT * WHERE pair_id EQUALS TO 1

Since I am not familiar with your specific code, my best guess is that you are making an asynchronous call to a PHP controller. Instead of hardcoding '1' in the query, make sure to use the variable from your code to specify what you are searching for.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with AES decryption on my nodeJS/ExpressJS server backend

Looking to decipher data post retrieval from mongoDb. The retrieved data comprises encrypted and unencrypted sections. app.get("/receive", async (req, res) => { try { const data = await UploadData.find(); const decryptedData = data. ...

Slow CSS :hover animations in the most recent release of Chrome

I recently upgraded my browser from chromium version 67 to the latest Chrome version 79. However, I noticed that after the upgrade, the CSS transitions on my website have become very laggy and unresponsive. Surprisingly, everything works perfectly fine on ...

What could be causing my selenium tests to fail on travis-ci even though there have been no code changes, when they are passing successfully

I'm facing a tough challenge trying to troubleshoot a selenium test that passes when run locally but not on travis. Reviewing the travis build logs, I noticed that the test was passing in build #311 but started failing at build #312. It seems like th ...

Guide on centering an element on a screen using Selenium and Python

I am currently working on automating a webpage with Robot Framework, Selenium, and Python. As part of the automation process, I need to scroll an element to the middle of the screen before clicking on it. Although I have tried using the following code: se ...

retrieve data from the API response using a node request

I initiated an API request using the request method. Everything seems to be functioning properly, but I am encountering difficulty extracting the results from the response. The snippet of my code in question is as follows: app.get('/events/:query&a ...

Create a plot marker on a map for every user's location based on their IP

Within my database, there exists a users table that stores each user's IP address. Additionally, I have an API that is capable of retrieving the latitude and longitude coordinates for these users. Initially, the goal is to fetch the latitude and long ...

Implementing html5mode in Express.js and Angular.js for cleaner URLs

I've been working on resolving the issue of avoiding # in my Angular app with an ExpressJS server-side setup. I found a solution to enable html5mode and it worked well. However, whenever there is another 'get' request to fetch data from a di ...

Exploring the world of React-Bootstrap elements and properties

I'm currently diving into a Bootstrap home project and it's my first time working with Bootstrap. I came across a tag that has an 'inverse' attribute among others like 'fixedTop', 'fluid', and 'collapseOnSelect& ...

What is the best method for incorporating two materials into a mesh that has already been rendered on the canvas?

I've been experimenting with adding both color and an image to a Three.js Mesh after it's already been rendered on the canvas. From what I understand, if I use the same material with both color and a map, they will blend together and there's ...

What is the best way to create a clickable <li> element from AJAX search results that display dynamic HTML?

Utilizing AJAX technology, I have implemented a live search feature that populates results from a database as the user types in a text field. The results are presented in an unordered list format. My goal is to allow users to click on an item within the li ...

Finding an element based on its styling attribute, such as its position on the left or right side

One particular block that caught my eye is the slider element: <div id="sliderDispo" class="slider slider-dispo" data-slider-init="" data-slider-color="#0077b5 #EC6E31 #E40B0B" data-slider-step="33" > <div class="slider__interval" ...

Retrieving the specific value of an input from a group of inputs sharing a common class

After extensive research, I have not found a suitable solution to my specific issue. I am creating a block of HTML elements such as <div>, <span>, <i>, and <input> multiple times using a for loop (the number of times depends on the ...

The data is not showing up in the mysql vertical menu

I am not an expert, but I am trying to learn PHP and MySQL in order to create a blog for our animal rescue center. My goal is to build a vertical menu using data from a MySQL database. While searching for help on another issue, I stumbled upon this site an ...

What is the best way to display a form within every row of a data table?

I am currently working on a page that retrieves data from a backend and displays it in a Datatable. One of the columns in my table is an input field with a default value fetched from the backend, and another column contains buttons. How can I implement a f ...

Navigating iFrames using Phantomjs Watir

Trying to access a login page using my script: Using PhantomJS and Watir. The issue with the login page is that the login textfields are within an iFrame: <iframe class="hiFrame" data-reactid=".0.0.0.1.0.1.0.0.$frame" src="https://instagram.com/accou ...

Contrasting methods of adding a value to an array state in React

Can you advise me on the most effective method for inserting a value into an array, as well as explain the distinctions between these two code examples? setOtoValue((current) => [ ...current, Buffer.from(arraybuf, 'binary').toString(' ...

Understanding the rationale for rendering Views with vuex dependencies in vueJS

I'm facing an issue with my API call in App.vue that populates the vuex store's state. The Home.vue component displays images based on the store's state, but console errors are thrown before the state is populated. I'm unsure about the ...

How to retrieve the filename from a URL using Node.js

In my Node.js script, I have the following type of link: 148414929_307508464041827_8013797938118488137_n.mp4.m4a?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_ohc=_--i1eVUUXoAX9lJQ-u&ccb=7-4&oe=60835C8D&oh=61973532a48cb4fb62ac6711e7eba82f& ...

Firestore generates an error stating that 'onSnapshot() must have 1 to 4 arguments'

I am having trouble retrieving all unread messages for the current user from Firebase. The issue arises when using onSnapshot() as it initially fetches the required data but fails to do so when a new document is added, resulting in an error. FirebaseErro ...

Ensure that a group of checkboxes is mandatory

I currently have a series of checkboxes set up like so: <label>What is your preferred movie genre?</label> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="genre1" name="genre" ...