This is my query:
$tsql = "SELECT *
FROM
dbo.messages m INNER JOIN
dbo.contacts c
ON m.contactID = c.ID
WHERE
assigned='{$_COOKIE['member_name']}'
ORDER BY
m.status DESC, m.ID DESC";
This returns the contact ID but not the message ID. How do I get it to show:
<?php echo $row['ID'] ?>
But with the message ID and not the contact ID in an HTML form?
you can access id of c table using cid, and id of m table as mid
like $row['cid'] and $row['mid']
$tsql = "SELECT m.*, c.*, c.id as cid, m.id as mid
FROM
dbo.messages m INNER JOIN
dbo.contacts c
ON m.contactID = c.ID
WHERE
assigned='{$_COOKIE['member_name']}'
ORDER BY
m.status DESC, m.ID DESC";
Related
I have following tables with columns :
1) users
user_id username
2) projects
p_id p_name
3) project_status
psdi p_id cdid cid sid short_list res_sent status_date
4) company
cid company_name
5) status
sid status_name status_order is_cv_sent
6) projects_log
pl_id cdid project_name p_id user_id status date_time
Now, in this projects_log table there are same p_id value exist.
I want to show all unique p_id with latest date_time. as DESC order where pl.cdid = $cdid
I am using following query but can't get the results. It's not showing me all unique p_id as date_time DESC order :(
$get_log = mysqli_query($link, "SELECT
pl.*,
u.username,
p.p_name,
c.company_name,
s.status_name,
ps.*
FROM projects_log AS pl
LEFT JOIN users AS u ON u.user_id = pl.user_id
LEFT JOIN projects AS p ON p.p_id = pl.p_id
LEFT JOIN project_status AS ps ON ps.p_id = pl.p_id
LEFT JOIN company AS c ON c.cid = ps.cid
LEFT JOIN status AS s ON s.sid = ps.sid
WHERE pl.cdid = '$cdid' GROUP BY pl.p_id
ORDER BY pl.pl_id DESC ");
Using below query you will able to achieve group by p_id and will able to get values of max date_time row.
$get_log = mysqli_query($link, "SELECT
pl.*,
u.username,
p.p_name,
c.company_name,
s.status_name,
ps.*
FROM (
select MAX(date_time) as MaxDateTime,p_id from projects_log group by p_id
) as mpl join projects_log AS pl on mpl.MaxDateTime = pl.date_time and mpl.p_id = pl.p_id
LEFT JOIN users AS u ON u.user_id = pl.user_id
LEFT JOIN projects AS p ON p.p_id = pl.p_id
LEFT JOIN project_status AS ps ON ps.p_id = pl.p_id
LEFT JOIN company AS c ON c.cid = ps.cid
LEFT JOIN status AS s ON s.sid = ps.sid
WHERE pl.cdid = '$cdid'
ORDER BY pl.pl_id DESC ");
I'm having trouble with getting the 3 latest comments from two different tables.
Here is my code, which works perfect with one inner join:
$query = mysql_query("SELECT COUNT(c.topic_id) AS ctid, COUNT(c.deck_id) AS dtid, f.id AS forumid, f.class AS forumclass, f.name AS forumname, f.url AS forumurl,
c.id AS commentid, c.user_id AS commentuser, c.user_name AS commentusername, c.date AS commentdate,
c.topic_id AS topicid, c.deck_id AS deckid
FROM ".$prefix."comment AS c
INNER JOIN ".$prefix."forum AS f
ON c.topic_id = f.id GROUP BY f.id
ORDER BY commentdate DESC LIMIT 3") or die(mysql_error());
This works fine it shows the 3 latest comments from the forum table, however I have comments in the decks table too, but when I add another JOIN to the query it just won't work anymore.
$query = mysql_query("SELECT COUNT(c.topic_id) AS ctid, COUNT(c.deck_id) AS dtid, f.id AS forumid, f.class AS forumclass, f.name AS forumname, f.url AS forumurl,
c.id AS commentid, c.user_id AS commentuser, c.user_name AS commentusername, c.date AS commentdate,
c.topic_id AS topicid, c.deck_id AS deckid, , d.id, d.url AS deckurl, d.name AS deckname
FROM ".$prefix."comment AS c
INNER JOIN ".$prefix."forum AS f
ON c.topic_id = f.id
INNER JOIN ".$prefix."decks AS d
ON c.deck_id = d.id
GROUP BY f.id ORDER BY commentdate DESC LIMIT 3") or die(mysql_error());
There's a comment table and in the comment table there is a topic_id column, which is equal to the forum table's id column and there's also a deck_id column which is equal to the decks table's id column.
Obviously that GROUP BY f.id isn't good with the two inner joins.
After this query I have while ($top = mysql_fetch_assoc($query)){ ... and then if ($top['deckid']==0) then print the topicid informations else print the deckid informations.
EDIT:
Comment table (only what is important to us now):
id, topic_id, deck_id
topic_id = forum table's id
deck_id = deck table's id
Obviously there is no comment to every forum topic.
E.g.: Forum ID 5 has 4 comments, then comment table e.g.: ID 1,2,3,4 has topic_id 4,4,4,4 and deck_id 0,0,0,0.
If there's no comment then there's nothing in the comment table. So forum ID 6 has 0 comments, then there is nothing in the comments table.
If deck ID 12 has 2 comments, then comment table e.g.: 5,6 has deck_id 12,12 and topic_id 0,0.
Forum table:
id
Deck table:
id
EDIT2: Solution (not too nice, but it works):
//count how many comments the latest 3 deck topic has
$new_comment_query = mysql_query("SELECT COUNT(c.deck_id) AS dtid, c.id, c.deck_id, c.date, d.id
FROM ".$prefix."comment AS c LEFT JOIN ".$prefix."decks AS d ON d.id = c.deck_id GROUP BY d.id ORDER BY date DESC LIMIT 3");
$new_one = mysql_fetch_array($new_comment_query);
//count how many comments the latest 3 forum topic has
$new_forum_query = mysql_query("SELECT COUNT(c.topic_id) AS ctid, c.id, c.topic_id, c.date, f.id
FROM ".$prefix."comment AS c LEFT JOIN ".$prefix."forum AS f ON f.id = c.topic_id GROUP BY f.id ORDER BY date DESC LIMIT 3");
$newer_one = mysql_fetch_array($new_forum_query);
//get all the comments
$comment_query = mysql_query("SELECT id, topic_id, deck_id, date FROM ".$prefix."comment ORDER BY date DESC LIMIT 3");
while ($comment = mysql_fetch_assoc($comment_query))
{
if($comment['topic_id']==0)
{
$deck_query = mysql_query("SELECT * FROM ".$prefix."decks WHERE id=".$comment['deck_id']);
while ($deck_comments = mysql_fetch_assoc($deck_query))
{
//print all the things!
}
}
elseif($comment['deck_id']==0)
{
$forum_query = mysql_query("SELECT * FROM ".$prefix."forum WHERE id=".$comment['topic_id']);
while ($forum_comments = mysql_fetch_assoc($forum_query))
{
//print all the things!
}
}
}
Try running this query:
SELECT * FROM
(
SELECT COUNT(c.topic_id) AS ctid, COUNT(c.deck_id) AS dtid,
f.id AS forumid, f.class AS forumclass, f.name AS forumname,
f.url AS forumurl,
c.id AS commentid, c.user_id AS commentuser, c.user_name AS commentusername,
c.date AS commentdate, c.topic_id AS topicid, c.deck_id AS deckid,
d.id, d.url AS deckurl, d.name AS deckname
FROM ".$prefix."comment AS c
INNER JOIN ".$prefix."forum AS f
ON c.topic_id = f.id
INNER JOIN ".$prefix."decks AS d
ON c.deck_id = d.id
ORDER BY commentdate DESC
) t1
GROUP BY t1.forumid
LIMIT 3
I first execute your complex JOIN without the GROUP BY which was causing problems. Then I SELECT everything from that temporary table, grouping by the forumid.
In all honesty, I'm surprised your server wasn't barfing on your first query, let alone the second, but hopefully my answer will help you out.
//count how many comments the latest 3 deck topic has
$new_comment_query = mysql_query("SELECT COUNT(c.deck_id) AS dtid, c.id, c.deck_id, c.date, d.id
FROM ".$prefix."comment AS c LEFT JOIN ".$prefix."decks AS d ON d.id = c.deck_id GROUP BY d.id ORDER BY date DESC LIMIT 3");
$new_one = mysql_fetch_array($new_comment_query);
//count how many comments the latest 3 forum topic has
$new_forum_query = mysql_query("SELECT COUNT(c.topic_id) AS ctid, c.id, c.topic_id, c.date, f.id
FROM ".$prefix."comment AS c LEFT JOIN ".$prefix."forum AS f ON f.id = c.topic_id GROUP BY f.id ORDER BY date DESC LIMIT 3");
$newer_one = mysql_fetch_array($new_forum_query);
//get all the comments
$comment_query = mysql_query("SELECT id, topic_id, deck_id, date FROM ".$prefix."comment ORDER BY date DESC LIMIT 3");
while ($comment = mysql_fetch_assoc($comment_query))
{
if($comment['topic_id']==0)
{
$deck_query = mysql_query("SELECT * FROM ".$prefix."decks WHERE id=".$comment['deck_id']);
while ($deck_comments = mysql_fetch_assoc($deck_query))
{
//print all the things!
}
}
elseif($comment['deck_id']==0)
{
$forum_query = mysql_query("SELECT * FROM ".$prefix."forum WHERE id=".$comment['topic_id']);
while ($forum_comments = mysql_fetch_assoc($forum_query))
{
//print all the things!
}
}
}
I am trying to fetch record of a customer's latest transaction. The query I am trying is this:
SELECT
food_rate,
ambiance_rate,
service_rate,
cost_rate
FROM
tbl_transaction t
INNER JOIN tbl_feedback f
ON t.fid = f.fid
WHERE date_time IN
(SELECT
MAX(date_time)
WHERE c_id = 1)
The output will be a single row only. But it is giving syntax error.
Try this:
SELECT
food_rate,
ambiance_rate,
service_rate,
cost_rate
FROM
tbl_transaction t
INNER JOIN tbl_feedback f
ON t.fid = f.fid
WHERE c_id = 1
ORDER BY date_time DESC
LIMIT 1;
//you have not written table name in your subquery, i have rewritten the query
SELECT
food_rate,
ambiance_rate,
service_rate,
cost_rate
FROM
tbl_transaction t
INNER JOIN tbl_feedback f
ON t.fid = f.fid
WHERE date_time IN
(SELECT
MAX(date_time) from tbl_transaction
WHERE c_id = 1)
Try this
SELECT food_rate, ambiance_rate, service_rate, cost_rate
from tbl_transaction t
inner join tbl_feedback f ON t.fid=f.fid
where c_id=1
order by date_time desc
limit 1
Please I want to add another table (Comments) to this query to count the number of comments for each article.
$query = "SELECT M.id,M.j_surname,A.j_user_id,A.id,A.jart_title, A.jart_description, A.jart_createddate, COUNT(C.artid) AS count_comments FROM jt_articles A, jt_members M, jt_article_comments C where M.id = A.j_user_id AND C.artid = A.id ORDER BY A.jart_createddate DESC
update:
something like that (you dont post the scheme of the comments table)
SELECT M.id,M.j_surname,A.j_user_id,A.id,A.jart_title, A.jart_description,
A.jart_createddate , COUNT(A.id) AS count_comments
FROM jt_articles A
left join jt_members M on M.id = A.j_user_id
left join jt_article_comments C on C.artid = A.id
group by A.id
ORDER BY A.jart_createddate DESC
Hello can anybody see why this query fails?
SELECT A.idAd, A.ads_in_Cat, A.title, A.currency, A.price,
A.in_dpt, A.description, A.featured FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset
But this one works:
SELECT *FROM ads
LEFT JOIN featured ON featured.ad = ads.idAd
INNER JOIN dept ON dept.id_dept = ads.in_dpt
INNER JOIN sub_cat_ad ON id_sub_cat = ads.ads_in_Cat
INNER JOIN cat_ad ON idCat_ad = sub_cat_ad.from_cat_ad
ORDER BY featured DESC LIMIT :limit, :offset
In the first one, I don't want all columns from the table "ads", the query returns only columns till ...FROM ads A.
If you specify to only select fields from A that's what you get: Only fields from A.
If you want fields from other tables too you have to specify them as well.
SELECT
A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, A.in_dpt, A.description, A.featured,
F.*,
D.*,
S.*,
C.*
FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset