Select a value and convert to random string [closed] - php

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

Related

How to search in MySQL exploded array data [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 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:%';

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.

how to join two tables from different databases? [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 8 years ago.
Improve this question
Database 1 : sandbox
table 1 : coordinates
+----------------------------------------+
|coord_id | section_name | station_number|
+----------------------------------------+
| 1 | A | A7 |
| 2 | B | B20 |
| 3 | C | C3 |
| 4 | D | D14 |
| 5 | E | E9 |
+----------------------------------------+
database 2 : phone
table 2 : workstations
I only have READ privilege
+----------------------------+
| ID | ws | pod |
+----------------------------+
| 1 | COMP123 | A07 |
| 2 | COMP345 | B20 |
| 3 | COMP567 | C03 |
| 4 | COMP891 | D14 |
| 5 | COMP444 | E09 |
+----------------------------+
PROBLEM:
I only have READ privilege on that second table of that database.
I want to join both tables so I can display the "ws" field on screen for my PHP script.
My "station_number" field values are written differently from the "pod" field
(they have a zero in front of the letter if it is a single digit after the letter) does it make a difference?
I've seen examples online on how to join but for some reason I can't get it to work.
Do I need to create an extra field on my main Table to store the field values from "ws" or what? I'm a bit confused.
Thanks in advance!
Yes, it makes a difference if there's a 0 in one and not in the other. You'll have to modify the value of one or the other. Something like SUBSTR(station_number, 0, 1) + LPAD(SUBSTR(station_number, 2), 2, '0').
And to join the tables from different databases, you just have to put the database name in front of the table name. Probably something like sandbox.coordinates.
So your query might look something like this:
SELECT
*
FROM
sandbox.coordinates c
INNER JOIN phone.workstations w
ON (SUBSTR(c.station_number, 0, 1) + LPAD(SUBSTR(c.station_number, 2), 2, '0')) = w.pod
If you can update one of your tables to make the columns match, then your query is as simple as:
SELECT
*
FROM
sandbox.coordinates c
INNER JOIN phone.workstations w
ON c.station_number = w.pod

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