I have a general question regarding the assigning of sql results to a arrays.
What should I do when I want to assign some results to an array and when I am joining two or more tables and some columns got the same name:
Example:
$sqlExample = "select u.first_name, o.first_name from tbl_user u join tbl_owner o on u.user_id = o.user_id where u.user_id = $user_id;";
...
$userFirstName[$var] = $result['first_name'];
$ownerFirstName[$var] =
I know, that this is not a great example, but I hope that you are understanding my question..
I thought I could use something like the table prefix for the results, but it didn't worked.
-- Just an example not the code I am using/
Alias your columns in the results:
select u.first_name as user_first_name, o.first_name as owner_first_name from ...
Then use those aliases in your code:
$userFirstName[$var] = $result['user_first_name'];
$ownerFirstName[$var] = $result['owner_first_name'];
$sqlExample = "SELECT
u.first_name AS user_first_name,
o.first_name AS owner_first_name
FROM tbl_user u
JOIN tbl_owner o ON u.user_id = o.user_id
WHERE u.user_id = $user_id";
$result['user_first_name'];
$result['owner_first_name'];
Related
Suppose I have two table
Table Name group
column 1: giver
column 2: acceptor
Table name userinfo
column 1: name
column 2: status
i want to select giver,acceptor and userinfo.status that from group table where giver or acceptor whose name is zakir that giver or acceptor exist in uerinfo table as name.
Need Help to write sql statement for taht query..
Thanks in advance... :)
What you are referring to is also known as the INNER JOIN clause in an SQL Statement.
Depending on the relationship you can create an INNER JOIN to potentially connect the two variables that are identical.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Extracted from W3Schools.com
Try the below query,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp JOIN userinfo as ui
on ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Or try this without join,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp, userinfo as ui
WHERE ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Try this:
SELECT `group`.`giver`, `group`.`acceptor`, `userinfo`.`status`
FROM `group`, `userinfo`
WHERE (`group`.`giver` = 'zakir' OR `group`.`acceptor` = 'zakir')
AND `userinfo`.`name` = 'zakir'
select g.giver, g.acceptor u.status
from group g, userinfo u
where u.name = 'zakir'
and (g.giver = u.name or g.acceptor = u.name)
It should do a part of the job.
This assumes you want the status for both the giver and the acceptor
May need a little tweaking for mysql syntax
Select giver, g.status, acceptor, a.status
FROM GROUP
join userinfo as g on group.giver = g.name
join userinfo as a on group.acceptor = a.name
where (giver = 'zakir' or acceptor = 'zakir')
I have two tables firm and contactdetails. I am trying to get the the firm name from firm and certain contact details from contactdetails. I am using $id =$_GET['id']; to get the id . In contactdetails i have fk_firm_id which is my foreign key. I am not sure how to use the inner join query. I am trying the following query:
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1`
FROM contactdetails JOIN firm ON contactdetails.fk_firm_id='$id'";
echo $sql;
$result = mysql_query($sql);
but i am not getting the correct firm. Can anyone help me with this query, please.
You should use like this JOIN firm ON contactdetails.fk_firm_id = firm.id
$sql=" SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1`
FROM contactdetails
JOIN firm ON contactdetails.fk_firm_id = firm.id
WHERE contactdetails.fk_firm_id = '$id'
";
$result = mysql_query($sql);
This is assuming that your firm table has a primary key called id
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM contactdetails JOIN firm ON `contactdetails`.`fk_firm_id`=`firm`.`id`
WHERE `firm`.`id` = '$id'";
echo $sql;
$result = mysql_query($sql);
There is a mistake about JOIN and WHERE statements:
$sql = "SELECT
f.name,
c.address_physical_line_1,
c.fax_1,
c.phone_1
FROM
contactdetails c JOIN firm f ON c.fk_firm_id= f.id
WHERE c.id = '$id'";
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM
contactdetails JOIN firm ON contactdetails.fk_firm_id=firm.id where
contactdetails.fk_firm_id='$id'";
you should join on a firm's field such as firm.id
syntax: FROM table1 LEFT JOIN table2 ON table1.field1 compopr
table2.field2 compopr is : "=","<",">","<=",">=","<>"
You are missing the WHERE clause that limits the result set to only the firm you're interested in; now you're getting all firms joined with a single contact details record.
.. where firm.id=$id
For new applications, please use a database API that has prepared statements, like mysqli or pdo.
Use the following query for inner join
$sql="SELECT firm.name ,address_physical_line_1 , fax_1 , phone_1 FROM
contactctdetails INNER JOIN firm ON contactdetails.fk_firm_id=$id";
I've got 2 tables, a join, a SELECT *. Both tables contain the field id, but I need to explicitly access one in this way:
$query = "SELECT * FROM #__docman as d JOIN #__users u ON d.dmmantainedby = u.id WHERE d.catid = 5 ORDER BY d.id ASC";
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach($rows as $row) {
echo $row->id ;
}
I tried
echo $row->d.id;
That didn't work..I know I could technically change my SELECT to call for the id's and use aliases but, there are a lot of fields I am fetching, hence the *. Is there another way?
You'll have to use aliases, the query stays short if you duplicate the data:
"SELECT *, d.id as did, u.id as uid FROM #__docman as d JOIN #__users u ON d.dmmantainedby = u.id WHERE d.catid = 5 ORDER BY d.id ASC"
And then:
$row->did
$row->uid
SELECT *, d.id AS id_alias FROM ... ? This will select duplicate columns but will still be pretty short query.
What's the correct syntax to achieve this using 1 query instead of 2?
$result = mysql_query(SELECT * FROM users);
while($row = mysql_fetch_array($result)){
$result = mysql_query(SELECT SUM(balance) FROM users_account WHERE uid=$row[id]);
}
You should inner join both tables and group by user table.
You can add more columns in SELECT clause, remember to add also same columns to GROUP BY clause to get a standard SQL statement.
$query = " SELECT u.uid, SUM(a.balance)
FROM users_account a
INNER JOIN users u
ON u.uid = a.uid
GROUP BY u.uid";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
...
Arrange $query concatenating strings if needed.
This will return all records form user_account table with user info :-
SELECT SUM(ua.balance),u.*,ua.* FROM users_account ua
left join users u on ua.users_account=u.id
GROUP BY u.id
$result = mysql_query(SELECT a.*, b.SUM(balance) AS user_balance FROM users a, users_account b WHERE uid=$row[id]);
Yes so im building an query from the advanced search form.
I have this:
$query = "SELECT * FROM users WHERE 1 ";
$query .= "AND sex = '$sex' ";
for now, next im going to have AND birthday.. but then i dont know how to do it, because users birthday is stored in users_profile
So please correct me, how can i:
$query .= "AND birthday in users_profile = '1'";
Is this possible even, or should i reconstruct it and put birthday in users instead..
update:
in users, the id column there, is binded with users_profileĀ“s uID.
So in users_profileĀ“s uID column, there is the users id.
I assume your users_profile table is linked to the users table?
SELECT u.*, up.birthday
FROM users u
INNER JOIN users_profile up
ON u.user_id = up.user_id
WHERE sex = '$sex'
Here an Inner Join is used. The reason we can use u instead of users and up instead of users_profile is because we have set up the aliases "users u" and "users_profile up"
You need to look at the syntax for JOIN.
You need a way to join individual related rows in the two tables, something like:
SELECT u.* FROM users u, users_profile p
WHERE u.sex = 'M'
AND p.birthday = '1'
AND u.userid = p.userid;
I don't understand why you have separate tables for user and for users_profile, but you need to JOIN the two tables:
SELECT U.*
FROM users U
LEFT JOIN users_profile P
ON P.uID = U.uID
AND P.birthday = '1'
WHERE U.sex = '$sex'
Very possible, given you have the foreign key to the users_profile table.
Let's say the primary key in the users table is named 'id', and the users_profile table contain a field called 'uid' which point to the users table, you'd normally create the query like this:
SELECT * FROM users u, users_profile p WHERE u.id = p.uid
AND u.sex = '$sex' AND u.birthday = 1