I am new to MySQL so describing problems in words is difficult and searching for solutions is extremely challenging.
This problem is best explained visually:
I want to select (as an array) exchange_pair_id's that share the same pair_id.
So in the above data, my MySQL query would return an object:
{ pair_id: 1, exchange_pair_id: [183, 1] }
I am aware this is a butchered question, but I do not know the words to search to solve this problem.
Updated for clarity/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 output 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] }
]
I am thinking a query like this , but I'm waiting your answer at comments.
Basically, you use GROUP BY to obtain in two different columns the values for each pair_id:
SELECT pair_id, MIN(exhange_pair_id) AS id1, MAX(exchange_pair_id) AS id2
FROM yourtable
GROUP BY pair_id;
Update version: Can you try this please on your data?
In this case MYSQL let you concat field using a separator (,)
SELECT pair_id, GROUP_CONCAT(exhange_pair_id) AS exhange_pair_id
FROM yourtable
GROUP BY pair_id
try select exchange_pair_id from yourtable where pair_id=1 then it will return array [1,183]
The SQL you are looking for would be:
SELECT * WHERE pair_id == 1
Without knowing what your specific code looks like, I can only guess as to how you are implementing a call to your database. I would assume you are doing some sort of async call to a PHP controller. So instead of using '1' in the query, you will need to use whatever variable you are pulling from your code to know what you are looking for.
Related
Hello i got a query that throw the this error : error: column "paraglider.idMaker" does not exist
const result = await client.query('SELECT *, "maker.is" AS "maker_id", "maker.name" AS "maker_name" FROM "paraglider" JOIN "maker" ON "paraglider.idMaker"="maker.id"');
There no error of typo
This work perfect in the CLI PSQL:
SELECT paraglider.*,
maker.id AS maker_id,
maker.name AS maker_name
FROM paraglider
JOIN maker ON "idMaker"=maker.id;
I got this result:
id | name | type | release_year | created_at | idMaker | maker_id | maker_name
----+--------+----------+--------------+---------------+---------+----------+------------
1 |Tonic 2 | all | 2019 | time_stamp_ex | 1 | 1 | Skywalk
2 |Arcus RS| all | 2017 | time_stamp_ex | 2 | 2 | Swing
I don't understand why my query work in the CLI of PSQL but not in my code.
I try to change:
'ON "paraglider.idMaker"="maker.id"'
in
'ON "idMaker"="maker.id"'
and the error change in err: error: column "maker.id" does not exist.
Ok i find a query who work :
SELECT paraglider.*, maker.id AS maker_id, maker.name AS maker_name FROM paraglider JOIN maker ON "idMaker"=maker.id
But don't understand where the problem come from...
Im having the following schema:
var lottary = new Schema({
userid : String,
start: Number,
end: Number,
time: Number,
});
and im writing a query that get the result of the winner.
if the rows are as follows:
| start | end |
| 2 | 4 |
| 5 | 99 |
| 100 | 999 |
and my number are 55, it would return the row with start 5, and end 99, cause 55 is between those numbers.
I know this are done with the following in MYSQL:
SELECT *
FROM lotto_tickets
WHERE 40 BETWEEN start AND end
But, how is this done within mongoose / mongodb?
Possible duplicate, take a look at this: How to find documents having a query-value within the range of two key-values
db.lottary.find({ "start": { "$lt": 55}, "end": { "$gt": 55}});
You can obviously change the number 55 to anything u want.
Simply I'm trying to make it ignore sorting if the cell has ?
this is my table:
ID | name | supply
1 | John | 12
2 | Kayle | ?
3 | Tim | 24
when you sort supply by asc
ID | name | supply
1 | John | 12
3 | Tim | 24
2 | Kayle | ?
when you sort supply by desc
ID | name | supply
3 | Tim | 24
1 | John | 12
2 | Kayle | ?
IS there a way to do it?
You need to use Absolute position sorting plug-in.
For example:
var nameType = $.fn.dataTable.absoluteOrder({
value: '?', position: 'bottom'
});
var table = $('#example').DataTable({
columnDefs: [
{ targets: 0, type: nameType }
]
});
You need to include absolute.js file in addition to DataTables CSS/JS files.
See this example for code and demonstration.
I was practicing my javascript with CodeFights and after I finished an exercise I saw this function as a result:
// Subject :
// Several people are standing in a row and need to be divided into two teams.
// The first person goes into team 1, the second goes into team 2,
// the third goes into team 1 again, the fourth into team 2, and so on.
// You are given an array of positive integers - the weights of the people.
// Return an array of two integers, where the first element is the total weight of
// team 1, and the second element is the total weight of team 2
// after the division is complete.
// Example :
// For a = [50, 60, 60, 45, 70], the output should be
// alternatingSums(a) = [180, 105].
// answer
alternatingSums = a => a.reduce((p,v,i) => (p[i&1]+=v,p), [0,0])
I don't understand what p[i&1]+=v,p means.
The & symbol is a bitwise binary operator.
To understand what would happen, you have to convert each item to binary.
| i (decimal) | i (binary) | i & 1 |
|-------------|------------|-------|
| 0 | 0 | 0 |
| 1 | 1 | 1 |
| 2 | 10 | 0 |
| 3 | 11 | 1 |
| 4 | 100 | 0 |
| 5 | 101 | 1 |
Effectively, every even number will be transformed to 0, and every odd number will be transformed to 1.
If I was trying to achieve that outcome, I personally would have used the modulus operator (%)
p[i%2] += v;
But that's just me.
The other part is that there are two statements separated by a comma:
(p[i&1]+=v,p)
That's saying "Perform this action, then return p. It's shorthand for:
alternatingSums = a => a.reduce((p,v,i) => {
p[i&1]+=v;
return p;
},
[0,0])
It looks for an element of the p array that has index of i&1 - it is a bitwise AND operation. Then, increments its value by a value of v variable. Finally, returns the value of p variable.
I'm trying to figure out how to create a nested JSON object, something like this:
company: "Company 1",
pricing: {
term: "1 year",
price: "$4.95",
term: "2 years",
price: "3.95"
},
I have two tables in MySQL, one called plans which is structured in this fashion
| id | company |
------------------
| 2 | company 1 |
and another table plans_pricing to represent the pricing data
| id | plans_id | term | price |
--------------------------------
| 1 | 2 | 1 year | $4.95 |
| 2 | 2 | 2 years| $3.95 |
I am using Laravel 4 to query the database and create json to send back to my ajax request. Here is the query, which is currently sending a server 500 error.
public function results()
{
$answers = $_POST['answers'];
$data = DB::table('plans')
->join('plans_pricing', 'plans.id', '=', 'plans_pricing.plans_id')
->select('plans.company', 'plans_pricing.price', 'plans_pricing.term')
->whereIn('plans.id', $answers)
->get();
echo json_encode($data);
}
I'm not sure why this query isn't working, but that isn't even why i'm asking this question. I need to know how to get a nested JSON object, when I create the join, I believe that I'll receive a separate object for each, like here:
| company | price | term |
------------------------------------
| company 1 | 4.95 | 1 year |
| company 1 | 3.95 | 2 years|
How can I make this SQL query return a nested JSON object like the one I describe above? I have been stuck on this problem for two days now and could really use some guidance. Thanks
UPDATE:
The server 500 error was fixed by changing echo json_encode to return Response::json($data);
I've never used Laravel but I think this should work:
$output = array();
$currentCompany = "";
foreach ($data as $datum) {
if ($datum->company != $currentCompany) {
$output[] = array();
// get a reference to the newly added array element
end($output);
$currentItem = & $output[key($output)];
$currentCompany = $datum->company;
$currentItem['company'] = $currentCompany;
$currentItem['rates'] = array();
}
$currentItem['rates'][] = array("price" => $datum->price, "term" => $datum->term);
}
json_encoded result:
[{
"company":"company 1",
"rates":[{
"price":4.95,"term":"1 year"
},{
"price":3.95,"term":"2 years"
}]
}]