I am creating message conversation script between two users in PHP MYSQLI.I want to know how to count unread message between two users.My script not showing anything.
my database pm table
id from_id to_id msg sent_date read
1 2 3 hi how are you? 2019-12-05 04:14:20 1
2 3 2 fine 2019-12-05 05:15:58 0
3 2 3 hi 2019-12-05 03:20:34 1
4 5 2 hi 2019-12-05 08:30:40 0
Here is my source code
<?php
$unread_messages = 0;
if (isset($_SESSION['userid'])) {
$session_id = $_SESSION['userid'];
}
$sql = ("SELECT COUNT(*) AS unread_messages
FROM pm
WHERE pm.to_id = ? and pm.from_id=from_id
AND read = 0");
if ($stmt = $con->prepare($sql)) {
$stmt->bind_param('i', $session_id);
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $unread_messages;
}
}
?>
I am creating message conversation script between two users.
I would phrase this as:
select sum(*) cnt_unread
from pm
where
read = 0
and (
(pm.from_id = :userid1 and pm.to_id = :userid2)
or (pm.from_id = :userid2 and pm.to_id = :userid1)
)
This gives you the count of unread messages between :userid1 and :userid2 (whether from user 1 to user 2 or the other way around).
SET #u1 = 2; -- id of first user
SET #u2 = 3; -- id of 2nd user
SELECT count(`read`) as unread FROM pm
WHERE `read` = 0 AND (from_id in (#u1,#u2) OR to_id in (#u1,#u2));
Related
I have a MySQL DB that resembles the following:
uid suid
1 5
1 6
2 5
5 1
5 2
I am giving it a single unique "uid" via the POST method, call it 1. What I need to do is return all "suid" where $uid "has" suid AND suid (as uid) "has" $uid (as suid.) So, in the above example, the script should only return 5.
I know my first step is
"Select * FROM table Where uid = $uid"
then maybe I have to loop through the results and query the DB WHERE suid = $uid.
I do not know how to do the second query. Any suggestions?
One option here would be to self join, with the join condition being that uid in one table matches suid in the other table, and vice-versa for the suid in the first table.
SELECT
t1.suid
FROM yourTable t1
INNER JOIN yourTable t2
ON t1.uid = t2.suid AND
t1.suid = t2.uid
WHERE
t1.uid = 1
Before applying the WHERE clause, the above query would return two records:
uid | suid (uid not selected)
1 | 5
5 | 1
The WHERE clause then chooses the first record, which is what we want, using the uid parameter which you pass it.
Demo here:
Rextester
this is what I came up with:
$uid = $_POST["UID"];
$myquery = "SELECT * FROM Table WHERE uid = '$uid'";
$result = $conn->query($myquery);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myquery2 = "SELECT * FROM Table WHERE uid = '" . $row['suid'] . "' AND suid = '$uid'";
$result2 = $conn->query($myquery2);
if ($result2->num_rows > 0) {
while($row = $result2->fetch_assoc()) {
echo $row["uid"].
"!##$";
}
}
}
}
$conn->close();
?>
I feel it is not pretty but it did the trick.
I can't figure-it out how to count all these dancers columns and echo with total of all dancers
id | dancer1 | dancer2 | dancer3 | dancer4, and so on..
---------------------------------------
1 alex michael dalm name
2 clare rose test
I have this for the start but is not working:
$counter = mysql_query("SELECT COUNT(*) AS id FROM table");
$num = mysql_fetch_array($counter);
$dancers = $num["id"];
echo "Total dancers: $dancers";
Any help is appreciated. Thanks!
Try this:
$counter = mysql_query("SELECT * FROM table");
$dancers = 0;
while($rows = mysql_fetch_array($counter)){
for($i = 1; $i <= 24; $i++){
$dan_id = 'dancer'.$i;
if($rows[$dan_id] != "" || $rows[$dan_id] != null )
$dancers++;
}
}
echo "Total dancers:". $dancers;
Note: Never design your database table like this.
I would actually save your dancers in a different (easier) way... For examle:
ID NAME SURNAME PHONE ....
1 Anna Brickstone 0975 ...
2 Jef Damen 0754 ...
That way you could use the following code to count tables:
$dancersCount="0";
$sql = "SELECT * FROM dancers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$dancersCount++;
}
} else {
echo "No dancers..";
}
echo"$dancersCount";
The counter will count +1 each time it finds a row (dancer)
If you really want to do it that way...
Then I don't really think there's an easy way to fix this... You will probably need to check how many columns you have in your database but that's not something i can help you with...
You need to change your table structure:
id | dancerNumber | name
1 1 alex
2 1 clare
3 2 michael
4 2 rose
5 3 dalm
6 3 test
7 4 name
8 4 dana
SELECT COUNT(*) AS id FROM table will return 8 dancers. If this is what you were looking for?
if you want to keep your structure then you need to do the following sql query
SELECT dancer1,
dancer2,
dancer3,
(CASE WHEN (dancer1 <> "" AND dancer1 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer2 <> "" AND dancer2 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer3 <> "" AND dancer3 IS NOT NULL) THEN 1 ELSE 0 END) AS COUNTER
FROM table
This will count all the non empty and non null columns and add a counter at the end of the table. this counter will then contain the number of dancers with your structure.
Full answer with your php code
$query = 'SELECT (CASE WHEN (dancer1 <> "" AND dancer1 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer2 <> "" AND dancer2 IS NOT NULL) THEN 1 ELSE 0 END +
CASE WHEN (dancer3 <> "" AND dancer3 IS NOT NULL) THEN 1 ELSE 0 END) AS COUNTER
FROM table'
$counter = mysql_query($query);
$num = mysql_fetch_array($counter);
$dancers = $num["COUNTER"];
echo "Total dancers: $dancers";
I have two data tables stock_incomes, stock_outcomes and stock_outcomes_fifo (the one I insert pre-calculated data):
stock_incomes (stores leftovers data)
id| Levtv
-----------
7 | 100
8 | 250
9 | 350
stock_outcomes (here is the point)
id| Quantity
--------------
1 | 150*
I have no problem when stock_outcomes.Quantity is less than 100 (min(Id) from stock_incomes, please see my code below) but I have no idea what code to write I could get calculations if outcome is >100. In my example I used 150 and I would like to get data in next table as:
stock_outcomes_fifo (the one I wish to insert pre-calculated data from the previous two tables)
id| IncomeId| OutcomeId| OutcomePart| Leftv
---------------------------------------------
1 | 7 | 1 | 100 | 0
2 | 8 | 1 | 50 | 200
Here is my code with question inside (see last part of the code):
<?php
include_once("config.inc.php");
include_once("db.class.php");
// stock_outcomes
$db = new db($host, $database, $user, $passwd);
$sql = "SELECT * FROM stock_outcomes WHERE Id = '1'";
$mas = $db->get_array($sql);
if($mas) {
foreach ($mas as $k => $v) {
$OutcomeId = $mas[$k]['Id'];
$OutcomeQuantity = $mas[$k]['Quantity'];
}
}
// stock_incomes
$sql = "select * from stock_incomes where Id = (select min(Id) from stock_incomes where Leftv > 0)";
$mas = $db->get_array($sql);
if($mas) {
foreach ($mas as $k => $v) {
$IncomeId = $mas[$k]['Id'];
$IncomeLeftv = $mas[$k]['Leftv'];
}
}
// insert into stock_outcomes_fifo
if ($OutcomeQuantity <= $IncomeLeftv) {
$OutcomePart = $OutcomeQuantity;
$FifoLeftv = $IncomeLeftv - $OutcomeQuantity;
mysql_query("INSERT INTO `stock_outcomes_fifo` (IncomeId,OutcomeId,OutcomePart,Leftv) VALUES ($IncomeId, $OutcomeId, $OutcomePart, $FifoLeftv)");
}
if ($OutcomeQuantity > $IncomeLeftv) {
// I have no idea what php function to use in this case... please give me direction, thank you...
}
?>
The question has been solved, here is the final working code in case someone might need it:
<?php
include_once("config.inc.php");
include_once("db.class.php");
// stock_outcomes
$db = new db($host, $database, $user, $passwd);
$sql = "SELECT * FROM stock_outcomes WHERE Id = '1'";
$mas = $db->get_array($sql);
if($mas){
foreach ($mas as $k=>$v) {
$OutcomeId=$mas[$k]['Id'];
$OutcomeBarCode=$mas[$k]['BarCode'];
$OutcomeQuantity=$mas[$k]['Quantity'];
}
}
/* - Start code */
if ($OutcomeQuantity > 0) {
$sql = "select * from stock_incomes where Leftv > 0 order by id asc";
$mas = $db->get_array($sql);
if ($mas) {
//filing stock_outcomes_fifo
foreach ($mas as $k=>$v) {
$IncomeId = $mas[$k]['Id'];
$IncomeQuantity = $mas[$k]['Quantity'];
$IncomeUnitPrice = $mas[$k]['UnitPrice'];
$IncomeLeftv = $mas[$k]['Leftv'];
$OutcomePart = min($OutcomeQuantity, $IncomeLeftv);
$FifoLeftv = $IncomeLeftv - $OutcomePart;
$FifoCost = $IncomeUnitPrice * $OutcomePart;
mysql_query("INSERT INTO `stock_outcomes_fifo` (BarCode,IncomeId,OutcomeId,OutcomePart,UnitPrice,Leftv,Cost) VALUES ($OutcomeBarCode, $IncomeId, $OutcomeId, $OutcomePart, $IncomeUnitPrice, $FifoLeftv, $FifoCost)");
mysql_query("UPDATE `stock_incomes` SET Leftv = ".$FifoLeftv." WHERE Id = ".$IncomeId);
$OutcomeQuantity -= $OutcomePart;
if ($OutcomeQuantity <= 0) break;
}
$OutcomeCostQuery = "select sum(Cost) as summ from stock_outcomes_fifo where OutcomeId = ".$OutcomeId."";
$OutcomeCost = mysql_query($OutcomeCostQuery);
$OutcomeCostResult = mysql_fetch_array($OutcomeCost);
mysql_query("UPDATE `stock_outcomes` SET Cost = ".$OutcomeCostResult["summ"]." WHERE Id = ".$OutcomeId."");
}
} /* - Finish code */
?>
Please help me let me explain with this.....
purchase table
id purchase_id product_id qty net_unit_cost created_at
-------------------------------------------------------------------------
1 1 1 10 10 2022-10-10
--------------------------------------------------------------------------
2 2 1 20 12 2022-10-10
Sale table
sale_id product_id qty net_unit_price created_at
1 1 11 15 2022-10-10
in this, if i sold '11' units then how can i subtract from the rows to get remaining units? i've to subtract '10' units from first row and '1' unit from second row...
Above is a scheme i drew. There's an ideal case, when content.user are groupped. But usually they are not groupped.
What i meant in this scheme is:
At the first step, i select users.monetos WHERE users.id = content.user
At the second step, i decrement users.monetos with every content.cpc value (2.1 , 2.2)
When simulating this:
select content.user (9)
select users.monetos Where users.id=content.users (15)
So we have 15 value for users.monetos for users.id=9, now we go back to content table
and:
decrement 15 value with 8 (content.cpc) (15-8=7 > 0 -> go to step 2)
decrement 7 (result from previous step) with 10 (content.cpc) (7-10=-3 <0 -> update
content set active='0' where content.id= (current id when a negative result obtained) )
And like this for every content.user
More extended - I want to select contet.* rows which have content.active = 1 (n). Having this data, SELECT users.monetos WHERE users.id=content.user from previous query.
And now, by maximum (n) steps i decrement users.monetos value by content.cpc value
and on the moment when **users.monetos=0 or less than 0, i want to update content and SET active='0'**
By words, i want like to share users.monetos amount to each content entry (content.cpc for each). And there's no more users.monetos make the current content
entry inactive. And do this vor every content.user
What i'he done at this moment is shown below. I now it looks really bad, but i already don't know what to do. Count on you guys. Thank you.
$query = "select content.id, content.cpc, conent.user, content.active from content a
join users b on a.user=b.id
group by b.id where a.active='1'";
/** cycle each user **/
foreach($rows = $connector->fetchArray($query) as $row ) {
$monetos = $row['monetos'];
$query = "select id, cpc from content where user={$row['id']}";
/** cycle each users content **/
foreach($contents = $connector->fetchArray($query) as $content) {
echo $monetos;
$monetos -= $content['cpc'];
if($monetos <= 0) {
$disable[] = $content['id'];
}
}
if( isset($disable) ) {
$connector->query("update content set active='0' where id in(".implode(',',$disable).")");
}
}
By using GROUP_CONCAT we group the IDs and CPCs separated by a comma for later use and with GROUP BY the user ID we will get a single row of result per user.
On the foreach we deduce each CPC from the MONETOS and from there we set who needs to be disabled to the $to_disable array that is later used to disable all the id's needed to.
$query = "SELECT b.id AS user_id,
b.monetos,
GROUP_CONCAT(a.id ORDER BY a.id DESC) AS content_ids,
GROUP_CONCAT(a.cpc ORDER BY a.id DESC) AS cpc,
FROM content a
JOIN users b
ON a.user = b.id
GROUP BY b.id";
$to_disable = array();
$to_enable = array();
foreach($rows = $connector->fetchArray($query) as $row)
{
$monetos = $row['monetos'];
$data = array_combine(explode(',',$row['content_ids']), explode(',',$row['cpc']));
echo "USER {$row['user_id']} currently have {$monetos}!<br>\n";
foreach ($data as $content_id => $cpc)
{
$monetos -= $cpc;
echo "USER {$row['user_id']} after CONTENT {$content_id} now have {$monetos}!<br>\n";
if ($monetos <= 0)
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} disabled!<br>\n";
$to_disable[] = $content_id;
}
else
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} enabled!<br>\n";
$to_enable[] = $content_id;
}
}
echo "<br>\n";
}
if (sizeof($to_disable) > 0)
{
$connector->query("UPDATE content
SET active = 0
WHERE id IN (".implode(',',$to_disable).")");
}
echo "UPDATE content SET active = 0 WHERE id IN (".implode(',',$to_disable).")<br>\n";
if (sizeof($to_enable) > 0)
{
$connector->query("UPDATE content
SET active = 1
WHERE id IN (".implode(',',$to_enable).")");
}
echo "UPDATE content SET active = 0 WHERE id IN (".implode(',',$to_enable).")";
Using your SQL dump this is what I get:
USER 9 currently have 15!
USER 9 after CONTENT 16 now have 10!
USER 9 after CONTENT 30 now have 5!
USER 9 after CONTENT 17 now have 4!
USER 9 after CONTENT 31 now have -1!
USER 9 should have the CONTENT 31 disabled!
USER 9 after CONTENT 18 now have -4!
USER 9 should have the CONTENT 18 disabled!
USER 9 after CONTENT 32 now have -9!
USER 9 should have the CONTENT 32 disabled!
USER 9 after CONTENT 20 now have -13!
USER 9 should have the CONTENT 20 disabled!
USER 9 after CONTENT 33 now have -18!
USER 9 should have the CONTENT 33 disabled!
USER 9 after CONTENT 21 now have -22!
USER 9 should have the CONTENT 21 disabled!
USER 9 after CONTENT 34 now have -26!
USER 9 should have the CONTENT 34 disabled!
USER 9 after CONTENT 22 now have -31!
USER 9 should have the CONTENT 22 disabled!
USER 9 after CONTENT 24 now have -36!
USER 9 should have the CONTENT 24 disabled!
USER 9 after CONTENT 26 now have -41!
USER 9 should have the CONTENT 26 disabled!
USER 9 after CONTENT 29 now have -45!
USER 9 should have the CONTENT 29 disabled!
USER 10 after CONTENT 28 now have 95!
USER 11 after CONTENT 27 now have -4!
USER 11 should have the CONTENT 27 disabled!
And the UPDATE result:
UPDATE content SET active = 0 WHERE id IN (31,18,32,20,33,21,34,22,24,26,29,27)
And here is the sample code used to read the data as is:
<?php
// Your database info
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
$con = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT b.id AS user_id,
b.monetos,
GROUP_CONCAT(a.id ORDER BY a.id DESC) AS content_ids,
GROUP_CONCAT(a.cpc ORDER BY a.id DESC) AS cpc
FROM content a
JOIN users b
ON a.user = b.id
GROUP BY b.id";
$result = $con->prepare($sql);
$result->execute();
if ($result->rowCount() == 0)
{
die('No data found...');
}
$to_disable = array();
$to_enable = array();
foreach($result->fetchALL(PDO::FETCH_ASSOC) as $row)
{
$monetos = $row['monetos'];
$data = array_combine(explode(',',$row['content_ids']), explode(',',$row['cpc']));
echo "USER {$row['user_id']} currently have {$monetos}!<br>\n";
foreach ($data as $content_id => $cpc)
{
$monetos -= $cpc;
echo "USER {$row['user_id']} after CONTENT {$content_id} now have {$monetos}!<br>\n";
if ($monetos <= 0)
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} disabled!<br>\n";
$to_disable[] = $content_id;
}
else
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} enabled!<br>\n";
$to_enable[] = $content_id;
}
}
echo "<br>\n";
}
if (sizeof($to_disable) > 0)
{
$ids = implode(',',$to_disable);
$sql = "UPDATE content
SET active = 0
WHERE id IN ({$ids})";
$disable = $con->prepare($sql);
$disable->execute();
echo "UPDATE content SET active = 0 WHERE id IN ({$ids})<br>\n";
}
else
{
echo "Nothing was disabled...<br>\n";
}
if (sizeof($to_enable) > 0)
{
$ids = implode(',',$to_enable);
$sql = "UPDATE content
SET active = 1
WHERE id IN ({$ids})";
$enable = $con->prepare($sql);
$enable->execute();
echo "UPDATE content SET active = 1 WHERE id IN ({$ids})";
}
else
{
echo "Nothing was enabled...";
}
$con = NULL;
Can you try two UPDATE statements?
UPDATE
users u JOIN
( SELECT user, SUM(cpc) AS cpc FROM content GROUP BY user) as c ON (u.id=c.user)
SET u.monetos = u.monetos - c.cpc;
UPDATE content AS c
SET c.active = 0
WHERE
(SELECT u.monetos FROM users u WHERE c.user = u.id) <= 0;
I didn't get if users.monetos was allowed to go negative or not. If not you can add an IF() check to the SET u.monetos
SQL Fiddle
I have a problem, it may be a simple fix to the issue, but I can't seem to figure it out. I am new to PHP and MySQL, so I'm reading everything everywhere, but lack of experience is very frustrating, as often times it takes ages to realize a small error. Please look at the following tables and read below the questions.
The PHP/mysql is in Joomla environment, I am trying to modify a plugin, so that is updates with values from different tables into a set of other tables, that were not originally intended, but all tables reside in the same database.
Table 1 vm_orders
---------------------------------------------
order_id user_id
---------------------------------------------
20 1
55 6
65 2
30 4
50 67
Table 2 vm_order_item
---------------------------------------------
order_item_id order_id order_item_sku
---------------------------------------------
20 20 1
55 55 35
65 65 60
30 30 22
50 50 3
Table 3 xipt_ users
---------------------------------------------------
userid Profiletype template
----------------------------------------------------
1 1 default
6 3 default
2 1 default
4 8 default
67 7 default
Table 4 community_fields_values
---------------------------------------------
id user_id field_id value
---------------------------------------------
1 1 55 Female
2 6 35 Cat
3 2 2 2
4 4 18 Texas
5 67 12 bike
What I need to is first of all get the order number according to the user that has place the order.
The userid variable is being passed from elsewhere in the script. That part is working fine.
So the user 67 has placed an order. These are the things I want to achieve.
Query 1: I want to get the "orderid" value from "order_id" column of vm_orders table (table 1); i will call the result "vmorderid" and use it in another query.
Query 2: Using the "vmorderid" from query 1 as the order_id value in the "order_id" column of vm_order_item table (table 2).
I want to get the order_item_sku value from the "order_item_sku" column of my_order_item table (table 2).
I will call the result "vmsku" and use it in another query.
Query 3: Using the "vmsku" from query 2 as the profiletype value in the "Profiletype" column of vm_users table (table 3).
I want to UPDATE the value of the "profiletype" column, with "vmsku" value.
Query 4: Using the "vmsku" from query 2 as the value in the "value" column of community_fields_values (table 4).
I want to UPDATE the value of the "value" column in my_fields_values (table 4) "vmsku" value.
Okay, I hope you are with me so far, I have tried a couple of queries, but it's not working.
Here is what I have so far:
Assuming the user it is being passed from a param field.
$userid = $this->params->get('userid', 'defaultValue');
function _vm_custom_order($vmorderId)
{
$vmorderId = $database->loadResult();
$database = JFactory::getDBO();
// query the db to see if the user is already a member of group
$vmorderId ="
SELECT MAX
`order_id`
FROM
#__vm_orders';
WHERE
`user_id` = '{$userid}'
";
$database->setQuery( $vmorderId );
$data = $database->loadResult();
return $data;
}
function _vm_sku($vmsku)
{
$vmsku = $database->loadResult();
$database = JFactory::getDBO();
// query the db to see if the user is already a member of group
$vmsku = "
SELECT
`product_sku`
FROM
#__vm_order_item';
WHERE
`order_id` = '{$vmorderId}'
";
$database->setQuery( $vmsku );
$data = $database->loadResult();
return $data;
}
function _add( $userid, $groupid, $vmsku)
{
$success = false;
$database = JFactory::getDBO();
if (!$allow_multiplegroups = $this->params->get( 'allow_multiplegroups', '1' )) {
// query the db to see if the user is already a member of ANY group
$database->setQuery("
SELECT
`profiletype`
FROM
#__xipt_users
WHERE
`userid` = '{$userid}'
");
$member = $database->loadResult();
// if so, do not execute
if (intval($member) > 0) {
return $success;
}
}
$already = plgAmbrasubsAddToXipt::_already( $userid, $groupid );
if (($already != $userid))
{
$database->setQuery("
SELECT MAX
`order_id`
FROM
#__vm_orders
WHERE
`user_id` = '{$userid}'
");
$vmorderId = $database->loadResult();
if ($database->query()) {
$success = true;
}
}
if (($already != $userid))
{
$database->setQuery("
SELECT
`product_sku`
FROM
#__vm_order_item
WHERE
`order_id` = '{$vmorderId}'
");
$vmsku = $database->loadResult();
if ($database->query()) {
$success = true;
}
}
// if they aren't already a member of the group, add them to the group
if (($already != $userid))
{
$database->setQuery("
UPDATE
#__xipt_users
SET
`profiletype` = '{$vmsku}'
WHERE
`userid` = '{$userid}'
LIMIT 1
");
if ($database->query()) {
$success = true;
}
}
return $success;
}
}
I also tried it this way:
function _add( $userid, $groupid, $vmsku)
{
$success = false;
$database = JFactory::getDBO();
if (!$allow_multiplegroups = $this->params->get( 'allow_multiplegroups', '1' )) {
// query the db to see if the user is already a member of ANY group
$database->setQuery("
SELECT
`profiletype`
FROM
#__xipt_users
WHERE
`userid` = '{$userid}'
");
$member = $database->loadResult();
// if so, do not execute
if (intval($member) > 0) {
return $success;
}
}
$already = plgAmbrasubsAddToXipt::_already( $userid, $groupid );
if (($already != $userid))
{
$database->setQuery("
SELECT MAX
`order_id`
FROM
#__vm_orders
WHERE
`user_id` = '{$userid}'
");
$vmorderId = $database->loadResult();
if ($database->query()) {
$success = true;
}
}
if (($already != $userid))
{
$database->setQuery("
SELECT
`product_sku`
FROM
#__vm_order_item
WHERE
`order_id` = '{$vmorderId}'
");
$vmsku = $database->loadResult();
if ($database->query()) {
$success = true;
}
}
// if they aren't already a member of the group, add them to the group
if (($already != $userid))
{
$database->setQuery("
UPDATE
#__xipt_users
SET
`profiletype` = '{$vmsku}'
WHERE
`userid` = '{$userid}'
LIMIT 1
");
if ($database->query()) {
$success = true;
}
}
return $success;
}
}
EDIT: I have now tried as suggested, to use JOIN to accomplish the task, so far no joy!
UPDATE
#__xipt_users
SET
`profiletype.#__xipt_users` = `product_sku.#__vmsku`
WHERE
`userid` = '{$userid}'
AND
(
SELECT `order_id.#__vm_orders`
FROM #__vm_orders, #__vm_order_item
LEFT JOIN #__vm_orders
ON #__vm_orders.`order_id` = #__vm_order_item.`order_id`
ORDER BY `order_id.#__vm_order` DESC LIMIT 1
WHERE
`user_id.#__vm_orders` = '{$userid}'
) AS #__vmorder_id
SELECT ` product_sku.#__vm_order_item`
FROM #__vm_order_item, #__vmorder_id
LEFT JOIN #__vm_order_item
ON `#__vm_order_item.order_id` = `#__vmorder_id.order_id`
WHERE
`order_id.#__vm_order_item` = `order_id.#__vmorder_id`
)
AS #__vmsku
LIMIT 1
");
Join Statements
I would suggest you start learning how to create Join Statements in MySQL.
Have a look at this website:
http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php
That way you are able to combine multiple queries into one. It will make this job a lot easier!
Piece of paper
Also it will help you to draw your database on a piece of paper to get a better overview of what you want to do. For example you can draw lines between the table fields you want to link.