I need to write an db query based on the Joomla jdatabase format, but I get stuck with writing the 2 'AND' parts for the native sql query below (I could not figure it out based on http://docs.joomla.org/Selecting_data_using_JDatabase).
How to include the 2 AND parts? Appreciate your help
//get userid of collective based on entered collective name, and get productid based on entered productname
//NATIVE MYSQL:
SELECT #__users.id, #__products.productid
FROM #__users INNER JOIN #__products
ON #__users.id = #__products.owner
AND #__users.name = $form_username
AND #__products.productname = $form_productname;
What I have sofar:
//Joomla Jdatabase query sofar:
$query
->select($db->quoteName(array('a.id', 'b.productid')))
->from($db->quoteName('#__users', 'a'))
->join('INNER', $db->quoteName('#__products', 'b') . ' ON (' . $db->quoteName('a.id') . ' = ' . $db->quoteName('b.owner') . ') ') //HERE I think some AND parts need to be included in some way?
->order($db->quoteName('a.created') . ' DESC');
I haven't tested this so please let me know if it works and if not, I will come back with an edit
->join('INNER', $db->quoteName('#__products', 'b') . ' ON (' . $db->quoteName('a.id') . ' = ' . $db->quoteName('b.owner') . ')
AND ' . $db->quoteName('#__users.name') . ' = ' . $form_username . '
AND ' . $db->quoteName('#__products.productname') . ' = ' . $form_productname)
Related
I am trying to get letters before numbers with a descending order and am finding it very difficult to do so.
I have tried using Case ordering
ORDER BY CASE WHEN ' . $this->sortbycast . ' LIKE \'%[a-z]%\' THEN 0 WHEN ' . $this->sortbycast . ' LIKE \'%[0-9]%\' THEN 1 ELSE ' . $this->sortbycast . ' END DESC
Regular expression as follows
' ORDER BY ' . $this->sortbycast . ' REGEXP \'^[a-z]\', ' . $this->sortbycast . ' desc';
and CAST()
' ORDER BY CAST(' . $this->sortbycast . ' AS UNSIGNED), ' . $this->sortbycast;
But none of these have had the desired result. All work correctly with ascending order. Any help would be appreciated. Thanks.
Update due to Strawberry's response
DB Fiddle here
My present query looks like this - reformatted for legibility:
SELECT p.*
, m.data
, m.id author_id
FROM anchor_posts p
JOIN anchor_post_meta m
ON m.post = p.id
WHERE p.category = 5
AND m.extend = 3
ORDER
BY CAST('anchor_post_meta.data' AS UNSIGNED) -- NOTE: THIS IS A STRING !?!
, m.data DESC
LIMIT 12;
The desired result
The desired result would be to have items show in the following order:
data
==============
A Title
B Title
C Title
D Title
70000
60000
45000
30000
25000
12000
Please see the following version of the DB Fiddle to see more clearly.
I am trying to execute a query through Joomla 2.5.
By using the following MySQL query, it performs correctly:
$query = "REPLACE INTO fa_usr (id, date_time, usr, clu) SELECT NULL, NOW(), " . $usr . ", " . $clu . " FROM DUAL WHERE (SELECT COUNT(*) FROM fa_use_clu WHERE usr=" . $usr . " AND clu=" . $clu . ") < 1";
Now, as I'm trying to use JDatabaseQuery methods, how the above mentioned query could be transformed correctly?
I have checked this link, which is from the official documentation, but seems I cannot find an appropriate solution.
Any ideas?
Thanks!
I hope that someone can tell me what I am doing wrong.
I would like to query my SQL table and receive back a ClassID and a ClassName.
The ClassID is a char(8) and holds values such as 123.45L1 or 350.12.
I am attempting to shorten that value so that I receive back 123 or 350 only.
Here is my code:
$classSelect = "SELECT LEFT(ClassID , 3), ClassName FROM Class GROUP BY ClassID";
$result = mysql_query($classSelect);
echo "<td><select multiple size='10' name='Class'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='Choice'" . $row['ClassID'] . " - " . $row['ClassName'] . "'>" . $row['ClassID'] . " - " . $row['ClassName'] . "</option>";
}
echo "</select></td>";
SELECT LEFT(ClassID , 3) AS somename, ClassName
and then refer it from PHP as $row['somename']
If you are seeking to extract the string up to the decimal point, and that might not always be in the same place, you can use MySQL's SUBSTRING_INDEX() function:
SELECT SUBSTRING_INDEX(ClassID, '.', 1) FROM Class
If you are seeking to extract the integer up to the first non-decimal character, you can simply CAST() the string and MySQL will do the rest:
SELECT CAST(ClassID AS UNSIGNED) FROM Class
The benefit of this latter approach is that the resulting column will be of the correct datatype.
My requirement is to pull records from mysql database which have just 5 mins left from the current time as per the one of the columns in the database. The column is has user inserted datetime.
date_default_timezone_set("UTC");
$utc_time = date("Y-m-d H:i:s", time());
echo "UTC Time: " . $utc_time . "<br>";
$result = mysql_query("select reminder_text, reminder_subject, reminder_date_time_utc $table_name where (TIME_TO_SEC (TIMEDIFF(reminder_date_time_utc , '$utc_time')) < 300) AND (TIMEDIFF(reminder_date_time_utc , '$utc_time')) > 0) ") or die(mysql_error());
here the reminder_date_time inside the TIMEDIFF function is the column name to pick up the DATETIME. Using this query I do not get the results but if I place the date instead of reminder_date_time it gives me the correct output. For example if I say TIMEDIFF('2013-07-12 11:05:00' , '$utc_time') it gives me the correct output. And this same value: 2013-07-12 11:05:00 is actually present in one of the rows of this column reminder_date_time_utc
Any advice where I am going wrong... Does TIMEDIFF function not accept column name as one of the parameters.
Do you forgot the FROM in your sql?
Why dont you try to do it like following:
$utc_time = date("Y-m-d H:i:s", time() - 30000);
and change the query to
'SELECT * FROM ' . $tablename . ' WHERE reminder_date_time_utc > "' . $utc_time . '"';
or use DATE_ADD() function :
'SELECT * FROM ' . $tablename . ' WHERE reminder_date_time_utc > DATE_ADD(NOW(), INTERVAL -'5' SECOND));
I want that when someone votes for article information, it gets inserted into two tables
(or run any two queries, does not matter, insert, update or select).
I am using Joomla! 2.5.0 Stable.
components/com_content/models/article.php
public function storeVote($pk = 0, $rate = 0)
when executing this query:
$db->setQuery(
'INSERT INTO #__content_rating ( content_id, lastip, rating_sum, rating_count )' .
' VALUES ( '.(int) $pk.', '.$db->Quote($userIP).', '.(int) $rate.', 1 )'
I want that the information in #__content table will be inserted too.
How do I achieve that?
I tried following, but it does not work:
$db->setQuery(
'INSERT INTO #__content_rating ( content_id, lastip, rating_sum, rating_count )' .
' VALUES ( '.(int) $pk.', '.$db->Quote($userIP).', '.(int) $rate.', 1 )'
// 'UPDATE #__content ' .
' SET testas2 = rating_sum + '.(int) $rate .
' WHERE content_id = '.(int) $pk
// 'INSERT INTO #__content ( testas2 )' .
' VALUES (7799)'
);
This is picture with the syntax:
http://i49.tinypic.com/1ruux0.jpg
I read about MySQL transaction, will it help me in this case? If yes, then what should the syntax should look like?
Any advice is much appreciated.
Try to run the following directly in the DB (use phpAdmin):
UPDATE edqfi_content_rating , edqfi_content
SET edqfi_content_rating.rating_count = edqfi_content_rating.rating_count + 1,
edqfi_content_rating.rating_sum = edqfi_content_rating.rating_sum + 3,
edqfi_content_rating.lastip = '88.119.189.154',
edqfi_content.testas2=edqfi_content_rating.rating_sum + 3
WHERE edqfi_content_rating.content_id = 13
AND edqfi_content.id= 13
and see if you get any errors.