Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

April 11, 2017

How to get table all field's value in single word/query?


- If you want to get table all field's value in single and simple SQL query then you need to write some simple word.

- Use this simple query

Example:

SELECT * FROM <tablename>

Enjoy!

February 22, 2017

How to get SQL group_concat comma separated value sort in ASC/DESC order?



- Here is simple example to explain how to get group concat comma seprated value into order by

Example :

SELECT field1,
  GROUP_CONCAT(first_name ORDER BY first_name ASC SEPARATOR ',')
  FROM tablename

Enjoy!

January 7, 2017

SQL - How to get between value from current timestamp with table timestamp?



- Here I explain with simple example to get between timestamp value from database value

Example:

$starttime = '1479612462'; (timestam value)
$endtime = '1479612462'; (timestam value)

SELECT * FROM <tablename> WHERE date(from_unixtime(<tablefield>)) between '".."' AND '".."'

Enjoy!

SQL - How to check database timestamp value with current timestamp



SQL - Check database timestamp value with current timestamp

- I find a solution for check sql database timestamp value with current timestamp.

Example:

If you want to get past value from current time:

- SELECT * FROM <tablename> WHERE <fieldname> >= UNIX_TIMESTAMP(currentdate())

Enjoy!

September 4, 2016

MySQL select query to inner join two table in single field.




MySQL select Query to inner joint two tables on single field.

SELECT CASE

WHEN STATUS_FLAG=1 THEN (select id from table2 where id=TABLE1.F_ID)

ELSE (select id from table3 where id=TABLE1.F_ID)

END


FROM TABLE1

MySQL select query with case and when condition.



Here I explain CASE and WHEN condition with MySQL select query.

MySQL Example:

SELECT CASE

          WHEN STATUS_FLAG=1 THEN ‘Yes’ ELSE ‘No’

           END AS STATUS_FLAG


FROM TABLE1


Enjoy!

July 14, 2016

Single SQL query with GROUP_CONCAT, DISTINCT and SEPARATOR.



Here is example for use GROUP_CONCAT, DISTINCT and SEPARATOR in single sql query,

Example:

SELECT group_concat(distinct (id) SEPARATOR ', ') AS ID FROM (tablename) WHERE 1=1

Enjoy!

December 15, 2015

PHP - SQL select query return array with used ID field as a key


                                               



In SQL select query while loop you can used ID fields as array key, it's simple. Here is example for that:

<?php
$selRes=mysql_query("SELECT id, name FROM TableName order by id DESC";
if(mysql_affected_rows()>0)
{
$data=array();
while($rowSale=mysql_fetch_array($selRes))
{
    $data[$row[0]] = $rowSale[1];
}
}

//Output
print_r($data);
array([1]=>'name1',[2]=>'name2',[3]=>'name3');

?>

Enjoy!

June 18, 2015

MySQL: why does "SELECT count()" return a value of array?




There are two solution to get total number of records from any table, Here are both explain in brief:


First one is - you can used COUNT() directly to select query like below e.g.

$Qr=mysql_query('select COUNT(*) from tablename');
$Qry=mysql_fetch_array($Qr);
echo $Qry;

It's display total number of records.

--------------------------


Second one is - 'mysql_num_rows' like below e.g.

$Qr=mysql_query('select * from tablename');
$Qry=mysql_num_rows($Qr);
echo $Qry;

It's display total number of records.



Enjoy!

June 17, 2015

MySQL select query to COUNT only unique values.



If you used join query on parent table and child table and you want to fetch unique records from both table then see the below example

SELECT COUNT(DISTINCT(t1.id)) FROM table1 t1 left join table2 t2 on t2.id=t1.t2id

Enjoy!

June 16, 2015

MySQL: PHP register script not working.


It's simple sript to select OR insert query with MySQL.

I copied the example from the PHP documentation.


<?php
    $sql='Type here any select OR insert query';
    $result = mysql_query($sql);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }
    mysql_close();
?>

For more result Click Here!

Enjoy!

May 27, 2015

MySQL find string and replace using query



Yes, its possible to find string from database and direct replace new string using SQL query.

See the following example.

"update your_table_name set fieldname = replace(fieldname, ‘string to find’, ‘new string which you want replace’)"

Enjoy!

The Future of Technology: Emerging Trends to Watch in 2025

Introduction As we navigate through 2025, the technological landscape continues to evolve at an unprecedented pace. Innovations that once se...