Mysql pattern recognition and update [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've got a mysql table - that contains a column of links
like www.example.com/?book=2
how do I select through the entire table - but extract the id, to put it into its own column? A kind of select, update query?
Would it be easier to just copy the column - then force it to an int type? Would that then leave me the id as a number?
What is the best solution for this.
SELECT id, sourcelink FROM books
UPDATE books SET id=1 WHERE sourcelink ='www.example.com?book=1';

You could do:
update books
set id = substring_index(sourcelink, '=', -1) + 0
where sourcelink like '%\?%=%';
This sets the id to whatever is after the = converted to a number. The where clause checks that the sourcelink has a structure similar to what you expect. This is a rather loose pattern. It could be made more specific by using regular expressions.

This is a mischievous but correct answer, if your id number always comes at the end of the text string.
UPDATE books
SET id = CAST(REVERSE(CAST(REVERSE(sourcelink) AS INT)) AS INT) AS id
WHERE CAST(REVERSE(CAST(REVERSE(sourcelink) AS INT)) AS INT) <> 0
This relies on the quirk of MySQL's casting of a string to an integer that causes it to understand the string value 99 red balloons as the integer value 99.

Related

how print unique values in 2 columns using php [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 5 years ago.
Improve this question
two columns
user_id purchase_item
1 watch
1 TV
2 watch
2 fridge
2 Toys
when i use distinct comand in sql
same as it is show data but i want to following
user_id:1
purchase_item:watch
tv
user_id:2
purchase_item:watch
fridge
Toys
means i want user id print only one time so how it is possible in php
plz help me and tell coding please
If you want to do it completely in Mysql you can use aggregation to get items as comma separated list per user
select user_id, group_concat(purchase_item) purchase_item
from table
group by user_id
this will give you results like
user_id purchase_item
1 watch,TV
2 watch,fridge,Toys
Also note using group_concat the result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet
But its better if you do this in php just get the ordered results and apply your logic to show user_id only once for multiple purchase_item related to that user
select * from table order by user_id asc

Random like order with mysql and PHP [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 6 years ago.
Improve this question
I use PHP and Mysql. I have a table that looks kind of like this:
id title
----------
1 my title
2 another title
3 The last title
Now I want to select them with a random like order.
I will need to use LIMIT because of the query size.
The random like order should always be the same random order every time.
Example result, every time
3 The last title
1 my title
2 another title
Do another query run:
3 The last title
1 my title
2 another title
The same random like result appear.
Possible solutions
Add a real random number stored as a new column generated by insert.
Some fancy SELECT query that does some magic.
Something else?
Why I want this is that I insert products, first from one site, then from another. In the result I want to present them as a mix.
It's not really random at all, but then again your request wouldn't work with random numbers. You want some sort of a hash of each record to use in the order by. You can probably find something better, but as a simple example you could use:
SELECT * FROM my_table ORDER BY MOD(id, 2);
Which you can see working here:
http://sqlfiddle.com/#!9/269a4/1/0
Use ORDER BY RAND().So your query should be :
select * from <tablename> where <cond> then ORDER BY RAND() limit <startlimit>, <end limit>

Group by and show specific row value [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 6 years ago.
Improve this question
I have this code to output the value in table i used group by to
group the same item code but serial does not show it only show
onevalue the 123321. The output should be 123321,4354
$invoicequery=mysqli_query($link,"Select SUM(INVOICE_ITEM_QTY_MX),INVOICE_ID_MX,INVOICE_ITEM_UPRICE_MX,INVOICE_ITEM_AMOUNT_MX,INVOICE_ITEM_SERIAL_MX,INVOICE_ITEM_DESC_MX,INVOICE_ITEM_CODE_MX from invoice WHERE BRANCH_CODE_MX='".$display_branchcode."'
and INVOICE_NO_MX='".$invoicecode."' GROUP BY INVOICE_ITEM_CODE_MX");
while($row=mysqli_fetch_array($invoicequery))
{
$invoiceid=$row["INVOICE_ID_MX"];
$itemcode=$row["INVOICE_ITEM_CODE_MX"];
$itemquantity=$row['SUM(INVOICE_ITEM_QTY_MX)'];
$unitprice=$row["INVOICE_ITEM_UPRICE_MX"];
$amount=$row["INVOICE_ITEM_AMOUNT_MX"];
$serialimei=$row["INVOICE_ITEM_SERIAL_MX"];
$itemdescription=$row["INVOICE_ITEM_DESC_MX"];
}
Ouput
In Database
It sounds like you are looking for MYSQL's GROUP_CONCAT() function method. Simply wrap the column name you wish to have returned as a comma separated list with that function like you have used MAX().
Example:
SELECT student_name,
GROUP_CONCAT(test_score)
FROM student
GROUP BY student_name;
This will return, for example,
student_name | test_score
Becky C. | 80, 77, 95
Note that this value will be truncated according to your db's group_concat_max_len setting.

MYSQL- Query to search using 'like' but searching only specific columns [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 7 years ago.
Improve this question
I'm using this query to select data:
mysql_query("select * from tenders where Heading LIKE '%{$key}%' OR Date LIKE '%{$key}%'");
now there is a field status which indicates whether tender is online or offline i.e shown on website or hidden.
I want to select only those tenders whose status is online.
Can anyone help me with this? I am unable to use multiple where clause where status=online
How can I write query for this?
You don't write multiple WHERE clauses, you write multiple conditions in the WHERE clause. (Exactly like your query already does with the two LIKE comparisons, though perhaps with some parentheses to specify the logic a little more explicitly.) Something like this:
SELECT
*
FROM
tenders
WHERE
(Heading LIKE '%{$key}%' OR Date LIKE '%{$key}%')
AND status = 'online'
Also, I think you mean that status is a column, not a row. Otherwise, this whole thing becomes much more confusing...
mysql_query("SELECT * FROM tenders WHERE Heading = '%{$key}%' OR Date = '%{$key}%'");
Using WHERE x = y OR z = a should work in your case, I am a little confused on exactly what you want but this should help!

How to store a random SQL query by its ID? [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
$randWord = mysql_query("SELECT * FROM words ORDER BY RAND() LIMIT 1");
After I call a random row from my table (which includes a word, its definition, and an id column) I want to store that word and definition so I can edit it if another button is pressed. From what I've seen online the common solution is to store it by its id. How do I store a random SQL query by its id?
Here's an example of where the pro tip
Never use SELECT * in software, instead give the names of the
columns you want.
would have helped you. If your words table has an id column, store that value.
RAND() and fetching by * is strongly not recommended because of it's bad performance.
Suggested example:
$total = mysql_result(mysql_query('SELECT COUNT(*) FROM words'), 0);
$randWord = mysql_fetch_array(mysql_query('SELECT wid, word, definition FROM words LIMIT '.rand(0, $total - 1).', 1'), 0);
Where mysql_result obtains the total number of records, mysql_fetch_array turns the result into an array. rand(0, $total - 1) generates a random number between available records, and then satisfy the [LIMIT start, limit] statement in the query.
You may build up your own DB class to reduce the code redundancy.
For your question, you should create a field as a primary key (or unique identifier). Like the 'wid' field I suggested above. Then you can output it as the value of a button and so on.

Categories