Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
need to make table with asc and desc columns like below from length column.
+----------+-------+-------+-------+
| length | ordNr | asc | desc |
+----------+-------+-------+-------+
| 11 | 0 | 11 | 119 |
| 99 | 1 | 110 | 108 |
| 5 | 2 | 115 | 9 |
| 4 | 3 | 119 | 4 |
+----------+-------+-------+-------+
Can it be achieved in SQL? I know how to do it in php, but maybe javascript solution is easier?
this is how i done it in jsfiddle
I can't see how you can do this in SQL for two reasons
The values in the 'asc' column are dependent on the row order
The values in the 'desc' column can't be calculated until you have calculated the total of all the rows.
It would be much easier to read the 'length' column into an array in your programming language and handle all the necessary calculations within that array.
Try:
select max(t1._length) _length, t1._ordNr,
sum(case when t1._ordNr >=t2._ordNr then t2._length end) _asc,
sum(case when t1._ordNr <=t2._ordNr then t2._length end) _desc
from myTable t1 cross join myTable t2
group by t1._ordNr
(Underscores added to column names to avoid clashes with SQL reserved words.)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My table structure as fallows
+----+------+---------+---------+
| id | name | heading | catid |
+----+------+---------+---------+
| 1 | ajay | xyz | 1:25:22 |
| 2 |sanjay| abc |15:25:45 |
+----+------+---------+---------+
If i get condition catid=22 then get result
+---+-----+----+---------+
| 1 | ajay| xyz| 1:25:22 |
+---+-----+----+---------+
If i get condition catid=15 then get result
+---+-----+----+----------+
| 2 | sanjay| abc|15:25:45|
+---+-----+----+----------+
If i get condition catid=25 then get result
+---+-----+----+----------+
| 1 | ajay| xyz| 1:25:22 |
+---+-----+----+----------+
| 2 | sanjay| abc|15:25:45|
+---+-----+----+----------+
You could use FIND_IN_SET, after replacing the colons in catid with commas:
SELECT *
FROM yourTable
WHERE FIND_IN_SET('25', REPLACE(catid, ':', ',')) > 0;
But a good long term investment would be to normalize the catid data and get those IDs in separate records.
There is also a way to do this using the LIKE operator, but it is ugly:
SELECT *
FROM yourTable
WHERE CONCAT(':', catid, ':') LIKE '%:25:%';
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a normalized table:
`Table: TheMovies`
id | MovieName
---------------------
1 | Zootopia
2 | Moana
3 | Toy Story
`Table: TheGenres`
id | GenreName
---------------------
21 | Action
22 | Animation
23 | Adventure
`Table: mMoviesGenres`
movieID | genreID
---------------------
1 | 21
1 | 23
2 | 22
2 | 23
2 | 21
3 | 23
All works fine, but I need a Query which will shoe me similiar movies based on at least one of the genres of MovieID = 1.
Can you give me an sql query so I have a basic idea of doing that, to be able to create more advanced queries?
To query using data from another table, you can join two or more tables into a single table by using JOIN clause.
SELECT TheMovies.* FROM mMoviesGenres JOIN TheMovies ON mMoviesGenres.MovieID = TheMovies.MovieID WHERE mMoviesGenres.MovieId <> 1 AND mMoviesGenres.GenreID IN (SELECT GenreID FROM mMoviesGenres WHERE MovieID = 1)
Learn more about join: Using Join to Retrieve Data from Multiple Tables
SELECT TheMovies.*
FROM mMoviesGenres
JOIN TheMovies
ON mMoviesGenres.MovieID = TheMovies.MovieID
WHERE
mMoviesGenres.MovieId <> 1
AND mMoviesGenres.GenreID IN (SELECT GenreID FROM mMoviesGenres WHERE MovieID = 1)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to select a value from my database table.
However, after select that value, I need to convert it to a random string which its length is only 9 character.
For example
SELECT dataValue FROM myTable ORDER BY id
The real dataValue
+------+
| 234 |
| 325 |
| 335 |
| 3e5 |
| 335 |
| 3w5 |
+------+
Now when I loop the dataValue from the database,
+-------------------+
| asdasdsavdcvx234 |
| 3fdgdfsdfsdfsg25 |
| 3asdaghfjktrse35 |
| 3ehgfhewrsdfsdf5 |
| 3342ret5432qwq35 |
| 343ty54ewrw23rw5 |
+-------------------+
All of them is unique.
Is there a way to do this? Thanks
SELECT SUBSTRING(MD5(dataValue), 0, 9) AS otherdataValue FROM myTable ORDER BY id;
Use
MYSQL
SUBSTR() and MD5()
you can use inbuilt mysql function md5() encode() and more
SELECT MD5(dataValue) FROM myTable ORDER BY id;
for more functions :- http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html
EDITED: try using:
select substr(md5(dataValue),1,10) as inst from myTable ORDER BY id;
NOTE: in mysql you should give index from 1 not from 0.using 0 won't return anything.
see here for more info:http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here is my problem...
table looks like this:
|id |coutry |sum
| 0 | USA | 30
| 1 | USA | 60
| 2 | USA | 90
| 3 | USA | 80
| 4 | GER | 40
| 5 | GER | 90
| 6 | SWE | 30
| 7 | SWE | 10
| 8 | SWE | 90
| 9 | SWE | 40
and result (top three scores if exists) should be like this:
|coutry |total
| USA | 230
| GER | 130
| SWE | 160
Any change to solve this by sql only? I'll fetch final result using php foreach..
The limit keyword should do the trick:
SELECT country, SUM(`sum`)
FROM my_table
GROUP BY country
ORDER BY 2 DESC
LIMIT 3
You need to establish a row number to get the top 3 sums per country. To do this with MySql, you have to use user-defined variables.
SELECT country, SUM(`sum`)
FROM (
SELECT *,
#rn:=IF(#prevCountry=country,#rn+1,1) rn,
#prevCountry:=country
FROM yourtable, (SELECT #rn:=0, #prevCountry:='') t
ORDER BY country, `sum` DESC
) t
WHERE rn <= 3
GROUP BY country
ORDER BY country
SQL Fiddle Demo
Try it here because #Mureinik have right.
SQL Fiddle
Order by country DESC solve "TOP" problem.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
how to use multi-keywords search with php and mysql ?
I have a product table like this
the keywords field is save the keyword id
| id | name | keyword_ids |
|112 | apple | 123,12,421,121|
|113 | phone | 23,14,12,1 |
and the keyword table like this
|id | name |
|1 | white |
|2 | eat |
I want use a product keywords field find the similar product, how can I do it?
If you're set on using this data structure, you can use FIND_IN_SET
SELECT * FROM `products` p
LEFT JOIN `keyword` k ON FIND_IN_SET(k.`id`, p.`keyword_ids`)
WHERE k.`name` IN (?,?,?)
What I'd recommend doing is actually from a many-to-many relation table linking a product to keywords eg:
product_has_keyword
product_id | keyword_id
------------------------
112 | 123
112 | 12
112 | 421
112 | 121
That way you can use index for a join (which will be much faster)