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]);
Related
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'];
In database the users have a row called weapon_id which determines what weapon he uses from another table with weapons.
Is there a better way to get that info like join table or something?
$user_get = mysqli_query($db, "SELECT * FROM members WHERE id = '".$_SESSION['sess_id']."'");
$user = mysqli_fetch_assoc($user_get);
$weapon_get = mysqli_query($db, "SELECT * FROM weapons WHERE weapon_id = '".$user['weapon_id']."'");
$weapon = mysqli_fetch_assoc($weapon_get);
use a join like this
$user_get = mysqli_query($db, "SELECT * FROM members m LEFT JOIN weapons w ON(w.weapon_id=m.weapon_id) WHERE m.id = '".$_SESSION['sess_id']."'");
$user = mysqli_fetch_assoc($user_get);
change the join type as per your requirement. the above query for only example
INNER JOIN: Returns all rows when there is at least one match in BOTH
tables
LEFT JOIN: Return all rows from the left table, and the matched rows
from the right table
RIGHT JOIN: Return all rows from the right table, and the matched
rows from the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables
more about join click here
AND also check this http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
SELECT * FROM `weapon` as w
JOIN `user` as u
on w.weapon_id = u.weapon_id
and u.id = :session_id
How about this ?
I have two queries. I have to execute both queries and return in same result object.
for example
$query1 = "SELECT name,age from students";
$query2 = "SELECT name,age from users";
$result1 = $this->db->query($query1)
$result2 = $this->db->query($query2)
return result1 and result 2 together ;
I have to return the result of both queries in same object. please help me
the actual query is ::
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id,dev_family.house_name,dev_ib_account_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_account_registration ON dev_ib_account_registration.member_id=dev_members.id
UNION
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id, dev_family.house_name,dev_ib_sub_member_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_sub_member_registration ON dev_ib_sub_member_registration.member_id=dev_members.id
You can use UNION
$query = "SELECT name,age from students
UNION
SELECT name,age from users";
$result = $this->db->query($query);
Or if you want to identify records from which table row belongs to then
$query = "SELECT name,age, 'students ' AS `table_type` from students
UNION
SELECT name,age, 'users' AS `table_type` from users";
$result = $this->db->query($query);
You might want to search for a question before asking it..
Here is the same question already with an answer.
$query = "SELECT name,age from students UNION ALL SELECT name,age from users";
You need to use UNION ALL instead of UNION in your query
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id,dev_family.house_name,dev_ib_account_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_account_registration ON dev_ib_account_registration.member_id=dev_members.id
UNION ALL
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id, dev_family.house_name,dev_ib_sub_member_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_sub_member_registration ON dev_ib_sub_member_registration.member_id=dev_members.id
UNION removes repeating rows in final result set while UNION ALL keeps the duplicate.
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.
I need to pull data from 2 tables in my database. The data I pull from table 2 depends on the result of table 1.
I'm not amazing at all these JOINS and things, so if someone could just explain what kind of a JOIN i'd need here, and how it would look, i'd be grateful:
$sql_result = mysql_query("SELECT * FROM accounts WHERE id='$val'", $db);
$rs = mysql_fetch_array($sql_result); $name = $rs[name];
$sql_result2 = mysql_query("SELECT * FROM players WHERE name='$name'", $db);
$rs2 = mysql_fetch_array($sql_result2);
$sql="SELECT * FROM accounts JOIN players ON accounts.accounts_link_to_player_id_here=players.id WHERE accounts.id='$val'";
You can do something like this, depending on the structure of the table(s):
SELECT * FROM `accounts` INNER JOIN `players` USING (`name`) WHERE `accounts`.`id` = 'value';
SELECT * FROM accounts LEFT JOIN players USING (name) WHERE accounts.id = 'value';
You'll need a query that looks like this (this is known in SQL as a subselect):
"SELECT p.* FROM players p WHERE name IN ( SELECT n.name FROM accounts n WHERE n.id = '$val' )"