When chinese word in mysql where statement [duplicate] - php

This question already has answers here:
What is the difference between varchar and nvarchar?
(21 answers)
Closed 5 years ago.
how to select 主页报修上广告
$sql = "SELECT text,pic FROM newstable where type='在线报修上广告' order by id desc limit 0,1";
but you may find the return value is null
But i try to use this
$sql = "SELECT text,pic FROM newstable where type=N'在线报修上广告' order by id desc limit 0,1";
there is N before '在线报修上广告' ------->>>>>but it works....what's the meaning of this 'N'!!

What is the meaning of the prefix N in T-SQL statements?
It#s declaring your varchar as nvarchar which is using a unicode encoding page. Otherwise it will be converted to the default encoding page of your database. Basically it's prefered to use only varchar when intended (like email addresses)

Related

MySQL How to order rows before grouping them? [duplicate]

This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 4 years ago.
I'm trying to return just 1 row for every size, brand and kind but sometimes there are multiple rows with the exact same values except enddate.
I want to return the row with the highest enddate (Order by enddate desc) doesn't work.
SELECT
promotion.id,
promotion.uid,
promotion.promotion_id,
promotion.url,
promotion.shop_id,
promotion.brand_kind_id,
promotion.before_price,
promotion.after_price,
promotion.brand_id,
promotion.brand_package_id,
promotion.brand_size,
promotion.amount,
promotion.begindate,
promotion.enddate
FROM
`promotion`
WHERE
AND (promotion.url is not null AND promotion.url != '')
GROUP BY
promotion.brand_id,
promotion.brand_kind_id,
promotion.brand_size
ORDER BY
promotion.enddate DESC
If the specific problem is that some rows have a different end date, I would recommend removing end date from your select string. This will mean that rows with different end dates will not be considered.
Another option might be to select the MAX end date, you could try this:
SELECT
promotion.id,
promotion.uid,
promotion.promotion_id,
promotion.url,
promotion.shop_id,
promotion.brand_kind_id,
promotion.before_price,
promotion.after_price,
promotion.brand_id,
promotion.brand_package_id,
promotion.brand_size,
promotion.amount,
promotion.begindate,
MAX(promotion.enddate)
FROM
`promotion`
WHERE
AND (promotion.url is not null AND promotion.url != '')
GROUP BY
promotion.brand_id,
promotion.brand_kind_id,
promotion.brand_size
ORDER BY
promotion.enddate DESC
However I would only recommend this method if you intend to use the end date as data.

Data from serverside not display [duplicate]

This question already has answers here:
LIMIT 10..20 in SQL Server
(15 answers)
Closed 3 years ago.
hello guys i try data use datatables from serverside (SQL Server) if me use code
my data not found or not availaible. this is my code
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
please help me
Use OFFSET and FETCH in SQL Server OFFSET FETCH Clause.
SELECT * FROM table ORDER BY column OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;
Possible duplicate: LIMIT 10..20 in SQL Server.

Selecting different rows from database each calendar day [duplicate]

This question already has answers here:
MySQL: Alternatives to ORDER BY RAND()
(9 answers)
Closed 6 years ago.
I have a MySQL database with 112 rows (with ID column from 1 to 112), I need to SELECT 6 random rows (doesn't matter if sequential) to show in PHP/HTML page and to be changed daily.
The only option I think is to depend on the current date.
Is there any solution ?!
Thank you ...
EDIT:
Question was solved. Although no one really understood what I want but I keep getting down votes.
The question was : 6 Random rows EVERY DAY not every page refresh or every call from DB.
But thanks for the amazing effort. Question solved.
The MySQL statement ORDER BY RAND() will order the matching rows randomly. Combined with LIMIT 6 you'll get six random rows.
See http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand for reference.
Just use ORDER BY RAND() to randomize the row order, then show the first 6:
SELECT * FROM Yourtable ORDER BY RAND() LIMIT 0,6;
Use following query
SELECT column FROM table
ORDER BY RAND()
LIMIT 6

MySQL, PHP - query is getting all records when datetime is used [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
I have query like this:
SELECT * FROM messages
WHERE recipients LIKE '%0688427893%'
AND 'dateadded' > '2015-05-13 12:52:57'
ORDER BY dateadded ASC
This should select only messages that are added after 2015-05-13 12:52:57, but instead of doing that, it is selecting all messages. What am I doing wrong here?
SELECT * FROM messages
WHERE recipients LIKE '%0688427893%'
AND dateadded > '2015-05-13 12:52:57'
ORDER BY dateadded ASC
If you want to escape a column name in MySQL then use backticks
`dateadded`
Using quotes turns it into a normal string
'dateadded'

Using a MySQL Query result as a PHP variable [duplicate]

This question already has answers here:
Read one column from one row from a MySQL database
(8 answers)
Closed 8 years ago.
I am having an issue declaring a PHP variable as a MySQL result.
Code:
$num = mysql_query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");
What it does it retrieve the latest entry in the database, which is an int, and I want it to store into the PHP variable $num. Is this possible?
Is this possible?
Yes, that is possible.
You should, however, use mysqli or PDO instead of the old mysql_ functions.
See: Why shouldn't I use mysql_* functions in PHP?
An example of how it could look:
$result = $mysqli->query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");
$num = $result->fetch_object()->num;

Categories