Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. const mysql = require('mysql');
  2.  
  3. const connection = mysql.createConnection({
  4. host : 'localhost',
  5. database : 'fss',
  6. user : 'root',
  7. password : '1111'
  8. });
  9.  
  10. const pool = mysql.createPool({
  11. host : 'localhost',
  12. database : 'fss',
  13. user : 'root',
  14. password : '1111'
  15. });
  16.  
  17. module.exports = {connection, pool};
  18.  
  19. const {connection, pool} = require('./config');
  20.  
  21. const Games = {
  22. selectPart: function(filtr, callback){
  23. pool.getConnection(function(error, connection){
  24. if(error){
  25. console.log("Error", error);
  26. callback(error);
  27. }else{
  28. let {id1, id2, table1, table2, column1, column2, value1, value2, beginningWith, amount} = filtr;
  29.  
  30. connection.query("SELECT id, (SELECT COUNT(??) FROM ?? WHERE ??.?? = ?" +
  31. " AND ?? = ?) as dislike, " +
  32. "(SELECT COUNT(??) FROM ?? WHERE ??.?? = ?" +
  33. " AND ?? = ?) as lik FROM ?? LIMIT ?, ?",
  34. [id2, table2, table1, id1, id2, column2, value2, id2, table2, table1, id1,
  35. id2, column1, value1, table1, beginningWith, amount], function(err, result){
  36. callback(null, result);
  37. });
  38. }
  39. connection.release();
  40. });
  41. }
  42. };
  43.  
  44. module.exports = Games;
  45.  
  46. app.get('/facts', function(req, result, next){
  47. var filtr = {
  48. id1: 'id',
  49. id2: 'id_fact',
  50. table1: 'facts',
  51. table2: 'rating',
  52. column1: 'is_like',
  53. column2: 'is_like',
  54. value1: true,
  55. value2: false,
  56. beginningWith: 0,
  57. amount: 5
  58. };
  59. Games.selectPart(filtr, function(err, res){
  60. if(err){
  61. console.log(err);
  62. }else{
  63. console.log(res);
  64. }
  65. });
  66. });
  67.  
  68. SELECT id, (SELECT COUNT(id_fact) FROM rating WHERE facts.id = id_fact AND
  69. is_like = false) as d, (SELECT COUNT(id_fact) FROM rating WHERE facts.id =
  70. id_fact AND is_like = true) as l FROM facts LIMIT 0, 5;
  71.  
  72. +----+------+------+
  73. | id | d | l |
  74. +----+------+------+
  75. | 23 | 0 | 2 |
  76. | 24 | 1 | 1 |
  77. | 25 | 0 | 0 |
  78. | 26 | 1 | 0 |
  79. | 27 | 0 | 3 |
  80. +----+------+------+
  81. 5 rows in set (0.00 sec)
  82.  
  83. [ RowDataPacket { id: 23, d: 0, l: 0 },
  84. RowDataPacket { id: 24, d: 0, l: 0 },
  85. RowDataPacket { id: 25, d: 0, l: 0 },
  86. RowDataPacket { id: 26, d: 0, l: 0 },
  87. RowDataPacket { id: 27, d: 0, l: 0 } ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement