how to select data from a table useing values from another table - php

I want to select data from a table called tbl_users using a value from another table called mergeing and column called donator_1.
I tried the following:
$result = $DBcon->query("SELECT tbl_users.username, tbl_users.email, tbl_users.Phone_number FROM tbl_users,mergeing WHERE mergeing.donator_1= tbl_users.user_id AND mergeing._id = 6");
while($row = $result->fetch_array()) {
echo '<b>' . $l['user_id'] .'</b><b>' . $l['username'] . '</b>';
}

You can try nested queries:
SELECT username, email, Phone_number
FROM tbl_users
WHERE user_id = (SELECT donator_1 FROM mergeing WHERE _id = 6 )
or an inner join :
SELECT username, email, Phone_number
FROM tbl_users
JOIN mergeing ON tbl_users.user_id = mergeing.donator_1
WHERE mergeing._id = 6

Related

Mysql returning more than one query error

Okay, below is the full php code for this
$sql = "
SELECT text
, creator
, (SELECT name
FROM accounts
WHERE id IN (SELECT creator
FROM groupcomments
WHERE `group` = '$viewgroupid'
) AS account_name
, (SELECT lastname
FROM accounts
WHERE id IN (SELECT creator
FROM groupcomments
WHERE `group` = '$viewgroupid'
)
) AS account_lastname
, (SELECT role
FROM accounts
WHERE id IN (SELECT creator
FROM groupcomments
WHERE `group` = '$viewgroupid'
)
) AS account_role
, (SELECT picture
FROM accounts
WHERE id IN (SELECT creator
FROM groupcomments
WHERE `group` = '$viewgroupid'
)
) AS account_picture
, (SELECT id
FROM accounts
WHERE id IN (SELECT creator
FROM groupcomments
WHERE `group` = '$viewgroupid'
)
) AS account_id
FROM groupcomments
WHERE `group`='$viewgroupid'
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$gc_text = $row['text'];
$u_name = $row['account_name'];
$u_lastname = $row['account_lastname'];
$u_userid = $row['account_id'];
$u_picture = $row['account_picture'];
$u_role = $row['account_role'];
include 'files/social/groupcomment.php';
}
//
}
Basically what this is supposed to do is to select data from the table accounts and from the table groupcomments then include a file which will echo the name and lastname, this works perfect when there's just one row in thetable groupcomments but as soon as i add another row i get the following error: Subquery returns more than 1 row in htdocs/group.php:134 Stack trace: #0 htdocs/group.php(134): mysqli->query('SELECT text, cr...') #1 {main} thrown in htdocs/group.php on line 134
and on line 134 is: $result = $conn->query($sql);
How can i get this to work with more than one row?
Why not simply:
$sql = "
SELECT gc.text
, gc.creator
, a.name AS account_name
, a.lastname AS account_lastname
, a.role AS account_role
, a.picture AS account_picture
, a.id AS account_id
FROM groupcomments gc
JOIN accounts a on gc.creator = a.id
WHERE gc.`group`='$viewgroupid'
";
Your error indicates that there are more than groupcomments.creator having group = '$viewgroupid' and you got the error because you cannot put several names within the same column in a row. With a join, this will not happen (you may get duplicate rows, but your particular query will not have that problem).
HTH,
Set
You may redesign your sql code ,
$sql="SELECT
text , creator, `ac`.name as account_name, `ac`.lastname as account_lastname, `ac`.role as account_role, `ac`.`id` as account_id
FROM groupcomments AS gp
INNER JOIN accounts as ac
ON `ac`.`id` =`gp`.`creator`
WHERE
`gp`.`group`='$viewgroupid'
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$gc_text = $row['text'];
$u_name = $row['account_name'];
$u_lastname = $row['account_lastname'];
$u_userid = $row['account_id'];
$u_picture = $row['account_picture'];
$u_role = $row['account_role'];
include 'files/social/groupcomment.php';
}
}

How can I turn these multiple mysql_querys into 1 query?

I have two tables: users and threads. When a thread is created, it will store the user_id in the table as author_id. I want to display the thread name and the username of its author with the same query. I am currently using two querys as shown:
$query2 = mysql_query("SELECT author_id FROM threads WHERE id = $threadId") or die(mysql_error());
$result2 = mysql_fetch_array($query2);
$author_id = $result2['author_id'];
$query3 = mysql_query("SELECT username FROM users WHERE id = $author_id") or die(mysql_error());
$result3 = mysql_fetch_array($query3);
$author_name = $result3['username'];
<?php
$sql = '
SELECT t.name, u.username
FROM threads t
JOIN users u ON t.author_id = u.id
WHERE t.id = ' . (int)$threadId . '
';
list($thread_name, $author_name) = mysql_fetch_row(mysql_query($sql));
P.S. Mysql php extension is deprecated.
Try this query:
SELECT username
FROM users
WHERE id = (SELECT author_id
FROM threads
WHERE id = $threadId
LIMIT 1)
Note: Limit 1 is not mandatory as id is unique.

MySQL Union Losing Table Names

I have a MySQL query where I am trying to search 2 tables simultaneously. This is for an autocomplete search box that searches for regular clients and business clients. Here is the code:
$query = mysql_query("SELECT * FROM clients WHERE lastname LIKE '$q%' AND agentid = '$agentid'
UNION
SELECT * FROM busclients WHERE busname LIKE '$q%' AND agentid = '$agentid'")or die(mysql_error());
if($query) {
while ($result = mysql_fetch_array($query)) {
$busname = $result['busname'];
print_r($result);
if(isset($busname)){
$description['id'] = 'viewbusiness.php?id=' . $result['ID'];
$description['value'] = $busname ;
array_push($return_arr,$description);
}
else
{
$description['id'] = 'viewclient.php?id=' .$result['ID'];
$description['value'] = $result['lastname'] . ", " . $result['firstname'] ;
array_push($return_arr,$description);
}
}
}
The problem is that the business clients get assigned the table names from the regular clients, so the code never uses the if(isset($busname)) because busname becomes lastname instead, and directs you to the veiwclient page.
SELECT
a.lastname ,
b.busname
FROM clients as a
INNER JOIN
busclients as b
on b.agent_id = a.agent_id
WHERE a.agent_id = $agent_id
AND (a.lastname LIKE '$q%' OR b.busname LIKE '$q%')
By specifically selecting the column names you want from that table, and selecting a blank string for the others, I believe you can get the results you're looking for using something like this:
SELECT firstname, lastname, '' AS busname FROM clients WHERE ... UNION SELECT '' AS firstname, '' AS lastname, busname FROM busclients WHERE ...

Joined 2 tables on ID which works fine, but now I want to echo the ID of the other table

I have 2 tables that have been joined using the ID that correlates the data on both tables.
I output the information perfectly.
I output ID and it returns that of the ID column in table_1 - PERFECT.
But now I want to output the ID column in table_2 within the same statement.
How do I now say ID from table_2, not table_1?
Here's some code...
$who = $_SESSION['who'];
$data = mysql_query("SELECT * FROM tbl_messages INNER JOIN tbl_users ON tbl_messages.from_user = tbl_users.id WHERE tbl_messages.to_user = $who")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
echo "From:" . $info['id'];
echo "to:" . $info['id'];
}
one good way to do this is to say
SELECT tbl_messages.id as msgID, tbl_users.id as usrID, * FROM tbl_messages INNER JOIN tbl_users ON tbl_messages.from_user = tbl_users.id WHERE tbl_messages.to_user = $who
Then you will reference the fields with that name
echo "From:" . $info['msgID'];
echo "to:" . $info['usrID'];

MYSQL like a join but only need the newest row?

I want to do the following but in one query:
$query = mysql_query("SELECT name FROM tbl_users WHERE category = '1'");
while($row = mysql_fetch_assoc($query))
{
$query2 = mysql_query("SELECT datecreated
FROM tbl_comments
ORDER BY datecreated DESC LIMIT 1");
$row2 = mysql_fetch_assoc($query2);
echo $row['name'] . " > " . $row2['datecreated'] ."<br />";
}
There is a user table and a comments table, I want to display a list of users and the date of the last comment next to them?
Is this possible in a single query?
You can do so using the following SQL:
SELECT name,
(
SELECT datecreated
FROM tbl_comments
WHERE tbl_users.user_id = tbl_comments.user_id
ORDER BY datecreated LIMIT 1
)
FROM tbl_users
WHERE category = '1';
OR using:
SELECT tbl_users.name, MAX(datecreated) AS latestcomment
FROM tbl_users LEFT JOIN tbl_comments ON (tbl_users.user_id = tbl_comments.user_id)
GROUP BY tbl_users.name;

Categories