How to search in MySQL exploded array data [closed] - php

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:%';

Related

Query for showing the Passers from the database [closed]

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 3 years ago.
Improve this question
Student_ID | First_Name | Last_Name | Examination_date | Exam_grade
| | | |
12345 | John | Doe | 12/01/2019 | 5
67890 | Johny | Bravo | 12/02/2019 | 7
09876 | Johnny | Boy | 12/02/2019 | 3
Hello Good day, I just want to show only the students who passed the exam, that has a minimum grade of 5 above. Thank you so much
here's my query
$conn->query("SELECT * FROM tbl_admission where exam_grade > 5");
use this condition - where exam_grade >= 5
SELECT * FROM tbl_admission where exam_grade >= 5
For having grade 5 or above you should use >= in your query .
SELECT * FROM tbl_admission where exam_grade >= 5

Compare results of two queries laravel [closed]

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 7 years ago.
Improve this question
I have two tables like below:
cat
+-------------------------------+
| id | 1 | 2 | 3 | 4 | 5 |
+-------------------------------+
| name | Hi | Ho | Hu | Ha | He |
+-------------------------------+
selected cat
+----------------+
| id | 2 | 5 |
+----------------+
| name | Ho | He |
+----------------+
Expected Output:
1 - > No
2 - > Yes
3 - > No
4 - > No
5 - > Yes
select id,
case when id in (select distinct id from selected_cat) then 'Yes'
else 'No' end
as somecol
from cat;
You can use a case statement to check for the existence of id in the other table.

Select a value and convert to random string [closed]

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

how to use multi-keywords search with php and mysql? [closed]

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)

how to sum colum row by row [closed]

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.)

Categories