aelia_co said
kingcoda said
aelia_co said
>
This query is inefficient and it won’t use indexes, if any. As a rule of thumb, one should never use “NOT”, or “different from” in the comparison clauses. A more efficient version is the following:
SELECT * from TABLE
WHERE registered < DATE_SUB(CURRENT_DATE(), INTERVAL 30 day)
First of all, it has nothing to do with indexes.
When you use SQL, it has always to do with indexes. Trust me on that, I’ve been a DBA for long enough.
kingcoda said
Besides, I know of famous frameworks which allows the use of NOT in comparison clause. Or <>
as per SQL-92 standard
The fact that they allow it doesn’t mean that it’s an optimal way of doing things. The “<>” clause, in particular, is a performance killer and should always be replaced by a more efficient one. You will also find dozens of frameworks that compare dates using strings (e.g. SUBSTR(DATE(field), 4) = ‘20150414’
), and that doesn’t make it a good practice (quite the opposite).
Another example is the method, that many people use even if they should not, of running a simple SELECT * FROM table
and then using foreach
in PHP to go through all the results and take the ones they want. That’s a terrible idea, especially when one nests queries in foreach loops (i.e. foreach…SELECT…foreach…SELECT). In the RDBMS world it’s called RBAR approach, and it’s frown upon (for a good reason).
Ah Jeez! Alright alright… mate… I take my words back
Yes, pulling all and then doing via scripting language is the worst thing to do. However, since you are a DBA, may I have an opportunity to ask…
At times, we want to implement a search engine (myisam based) which returns the results grouped by - alphabet-match first… ordered alphabetically… however, if I want to push special characters at the bottom, (during ASC on the row)… I faced this issue last month and ran it past php to push the special characters based result at the bottom… For example, if ‘alpha’ is searched… it should return… alpha, alphabet, alpha-bet and not alpa,alpha-bet, alphabet.
how could we achieve that using sql-only approach?
timmylogue said
timmylogue said
codents said
You can also do this with PHP.
$sql = "SELECT * FROM yourtable";
$query = mysqli_query($dbconnectvariable, $sql);
Thank you!!
Hey!
If you could help me out with one more thing! How would I echo out the results for this? I guess I would use some loop too!
I’m trying to display the “username(s)” of whoever has the value “expired” in their row. I have tired to Google some help before posting but I just can’t find out how to do this!
I was hoping this would work? I’m I close?
$result = mysql_query("SELECT * FROM users WHERE 'username'");
while ($row = mysql_fetch_array($result))
{
$whoexpired = $row['trial_date_ended'];
}
echo $whoexpired;
Thanks
And you see only the last username
That is because you are ‘echo’-ing it outside the while loop, at each loop, $whoexpired is getting re-assigned based on the number of rows.
Make that:
$result = mysql_query("SELECT * FROM users WHERE 'username'");
while ($row = mysql_fetch_array($result))
{
$whoexpired = $row['trial_date_ended'];
echo $whoexpired;
}
mysql_free_result($result);
Also, since I presume you are new and learning, dive into MySQLi rather mysql_ functions… mysql_ functions are deprecated as of php 5.5 and will be removed for good…
Happy learning