I'm looking for a way to update the data in 2 tables with an inner join, but it doesn't work for me.
What I want is to update the data of the dbo.UserInfo table and at the same time the Coin field of the dbo.UserInfovariable table.
UserInfo table
UserIndex
UserID
UserPassword
Nickname
1
admin
123456
UserPHP
UserInfoVariable table
UserIndex
Experience
Coin
1
989981454
§8000
I have tried with this sentence, it only works with "a", when I add b.Coin the sentence stops working
$sql = "UPDATE a
SET a.UserID = '$data[1]',
a.UserPassword = '$data[2]',
a.NickName = '$data[3]' ,
b.Coin ='$data[4]'
FROM UserInfo as a
INNER JOIN UserInfoVariable b ON a.UserIndex = b.UserIndex
where a.UserIndex = '$data[0]'";
Related
I’ve a BD with the next tables.
TABLE detalle_contrato
TABLE detalle_tradicional
There is a relation with ID_CONTRATO and i need to view the table with the next data.
SELECT
ID_CONTRATO,
TRADICIONAL,
NOM_VARIEDAD,
SUM(CANTIDAD)
FROM detalle_contrato
WHERE ID_CONTRATO = '$ID' AND TIPO_VARIEDAD = 'TRADICIONAL';
SELECT
SUM(CANTIDAD_D)
FROM detalle_tradicional
WHERE ID_CONTRATO = '$ID'
GROUP BY NOM_VARIEDAD ";
There are a filter different in this two select and I need this in a table but i don't know together.
The idea is this:
ID_CONTRATO,
NOM_VARIEDAD,
CANTIDAD
( THIS IS THE SUM THE ALL CANTIDAD DUKE AND
LEGACY IN GROUP THE TABLE DETALLE_CONTRATO) ,
CANTIDAD_D
(TABLE DETALLE_TRADICIONAL THIS IS SUM
THE ALL DUKE AND LEGACY SEPARATE THE CANTIDAD_D
I need exactly this using the data the photos
You can use LEFT JOIN. Left join your second table with id_contrato and detalle_contrato id_contrato.
SELECT
dc.ID_CONTRATO,
dc.TRADICIONAL,
dc.NOM_VARIEDAD,
dc.IFNULL(SUM(CANTIDAD),0) AS CANTIDAD,
dc.IFNULL(SUM(CANTIDAD_D),0) AS CANTIDAD_D
FROM
detalle_contrato dc
LEFT JOIN TABLE_NAME t2 ON t2.ID_CONTRATO = dc.ID_CONTRATO
WHERE dc.ID_CONTRATO = '$ID' AND t2.TIPO_VARIEDAD = 'TRADICIONAL'
I am having 2 tables :
1.internal_employee_master
id employee_name unique_id
1 Noah ABCD
2 Liam ABCD
3 William ABCD
4 Benjamin ABCD
5 Jacob EFGH
2.external_employee_master
id name unique_id
1 Elijah ABCD
2 Ethan ABCD
3 Alexander EFGH
I am using UNION query to get both tables data into single table and display this data into html table.
select id
, employee_name
, unique_id
from internal_employee_master
where unique_id = 'ABCD'
union
select id
, employee_name
, unique_id
from external_employee_master
where unique_id = 'ABCD'
I want to store payslips of both employees into single table.
I have one table payslips with emp_id and emp_type columns.
I am storing data into payslips data like:
id pay_slip emp_id emp_type
1 Noah_payslip.pdf 1 internal
2 Liam_payslip.pdf 2 internal
3 Lia_payslip.pdf 1 External
as you can see in above table i am storing emp_id and emp_type of
both the tables in single columns each.
Now, i dont undestand how to split data of internal employee and
external employee from pay_slip table and show data in html table.
Currently, i am writing below sql joins to get employee_names of
internal and external employee tables but it doesnt work for me.
$id = $_GET['id];
SELECT ps.id,ps.pdf,ps.emp_id,ps.emp_type,external_employee.name as comemp,
internal_employee.comp_empl_name as comemp
FROM pay_slip as ps
INNER JOIN internal_employee_master as internal_employee ON internal_employee.comp_trad_id = ps.trade_id
INNER JOIN external_employee_master as external_employee ON external_employee.trad_id = ps.trade_id
where ps.is_deleted = 1 AND ps.id = '".$id."'"
Please help me to join query to get name and employee_name with respect to emp_type form pay_slip table.
How about using UNION again?
SELECT
ps.id,
ps.pdf,
ps.emp_id,
ps.emp_type,
external_employee.name AS comemp,
internal_employee.comp_empl_name AS comemp
FROM
pay_slip AS ps
INNER JOIN
internal_employee_master AS internal_employee ON internal_employee.comp_trad_id = ps.trade_id
WHERE
ps.is_deleted = 1 AND ps.id = '".$id."'
AND ps.type = 'internal'
UNION ALL
SELECT
ps.id,
ps.pdf,
ps.emp_id,
ps.emp_type,
external_employee.name AS comemp,
internal_employee.comp_empl_name AS comemp
FROM
pay_slip AS ps
INNER JOIN
external_employee_master AS external_employee ON external_employee.trad_id = ps.trade_id
WHERE
ps.is_deleted = 1 AND ps.id = '".$id."'
AND ps.type = 'external'
You could try this
SELECT ps.id, ps.pay_slip, ps.emp_type, COALESCE(i.employee_name, e.name) AS name
FROM payslips ps
LEFT JOIN internal_employee_master i ON i.id = ps.emp_id AND ps.emp_type = 'internal'
LEFT JOIN external_employee_master e ON e.id = ps.emp_id AND ps.emp_type = 'External'
AND ps.id = :ID
You can see this in action here http://sqlfiddle.com/#!9/53a195/7/0
I would mention that there are a number of issues in your included tables and queries. For example, irregular column names between tables (name vs. employee_name), you've missed the is_deleted column from your example schema, and you have capitalised and non-capitalised values in the emp_type column which is confusing.
I have the following as my results.. the problem is I only want 1 field from the balances table and 2 fields from the userinfo table... if I replace the * with say user,avatar I get my error...
$result = mysql_query("SELECT * FROM userinfo INNER JOIN balances ON userinfo.user = balances.user ORDER By balance DESC,avatar");
if (!$result) {
die("Query to show fields from table failed");
I do not know the proper form and cant find it anywhere
TIA
John
as user column exists in both the joined tables userinfo and balances you need to prefix the table name while accessing the column
Try
SELECT userinfo.user, userinfo.avatar, balances.balance
FROM userinfo
INNER JOIN balances
ON userinfo.user = balances.user
ORDER By balance DESC,avatar
I have 2 tables.
Table A: trades: which contains the columns: tradeID, tradeName, tradeShow, and tradeGuy.
Table B: offers: which contains the columns: tradeID, offerName, offerGuy.
I'm trying to select all columns from table A (trades) WHERE the value of "tradeShow" = 'Yes', And the value of "tradeGuy" != the user's Username. That much is easy, but I also don't want to select any records which have an offer created by the user. In other words, in table B (offers), offerGuy != Username WHERE trade ID from Table B = tradeID from Table A.
But, how do I merge these 2 conditions? I've tried this:
$sql = "SELECT *
FROM trades t1
JOIN offers t2
ON (t1.tradeID = t2.tradeID)
WHERE t1.tradeShow='Yes' AND t1.tradeGuy!='$username' AND t2.offeringGuy!='$username'";
But the problem with that is it only selects the records from trades which have an offer, because of the forth line: ON (t1.tradeID = t2.tradeID), as in it only selects trades which have a record in (offers) that mentions their tradeID.
I've also tried an awkward attempt to link the 2 tables with a meaningless link by adding a "linker" column to each table with the default value of "XXX", and did this:
$sql = "SELECT *
FROM trades t1
JOIN offers t2
ON (t1.linkerA = t2.linkerB)
WHERE t1.tradeShow='Yes' AND t1.tradeGuy!='$username' AND (t2.offeringGuy!='$username' WHERE t1.tradeID=t2.tradeID)";
But the problem with that is using 2 Where clauses...
So, how do I merge the 2 conditions?
What you're looking for is called an OUTER JOIN (in this case a LEFT OUTER JOIN) which will give you null results for missing matches, something like;
SELECT *
FROM trades t1
LEFT OUTER JOIN offers t2
ON t1.tradeID = t2.tradeID AND t2.offeringGuy = '$username'
WHERE t1.tradeShow='Yes' AND t1.tradeGuy!='$username' AND t2.offeringGuy IS NULL
We add a condition to the LEFT JOIN that we're only interested in matches against t2.offeringGuy = '$username', which will return NULL values in t2's fields if there is no match.
Then we just check that t2.offeringGuy IS NULL to find the non matches.
I would do this with not exists rather than an explicit join:
SELECT *
FROM trades t
WHERE t.tradeShow = 'Yes' AND t.tradeGuy <> '$username' and
not exists (select 1
from offers o
where t.tradeID = o.tradeID and o.tradeGuy = '$username'
);
Hi Im new to MySQL and PHP, and this is my first post.. So please bear with me.
I am trying to add two tables and two rows from one table
friends
member_ID | friend_ID | status
and a user table
member_ID | username
What I am trying to do is combine both tables for a friend request, friends.member_ID is the user.member_ID sending the request, friends.member_ID, is the user.member_ID that is being requested, status is when they accept the request it will turn 0 to 1 which will make the relationship true.
so far, I have this for my query to display all these fields to show who has requested this person as a friend
SELECT users.member_ID, friends.member_ID, friends.friend_ID, friends.status
FROM `users` , `friends`
WHERE friends.status=0
AND users.member_ID = friends.member_ID
AND friends.member_ID = users.member_ID
AND users.member_ID = 6 (this will be $_SESSION[member_ID] when I add it to php)
I understand you can use an alias but I am a bit confused
Please help, my assignment is due tomorrow, and there is still so much to do.
Thanks
try this with join
SELECT users.member_ID, friends.member_ID, friends.friend_ID, friends.status
FROM `users`
INNER JOIN `friends`
ON users.member_ID = friends.member_ID
WHERE friends.status=0
AND users.member_ID = 6
Column alias - use AS(/as) after column name
users.member_ID as uId, friends.member_ID as fId
Table alias - define after table name
users u , friends f
If using table aliases, you can use them when selecting your column names
u.member_ID as uId, f.member_ID as fId
http://dev.mysql.com/doc/refman/4.1/en/problems-with-alias.html
SELECT u.member_ID as uId, f.member_ID as fId, f.friend_ID as ffId, f.status as fStatus
FROM `users` u
INNER JOIN `friends` f
ON u.member_ID = f.member_ID
WHERE f.status=0
AND u.member_ID = 6
Aliases are useful when you are selecting 2 or more columns with the same name from multiple tables - users.member_ID, friends.member_ID. Instead of using an ambiguous $row['member_ID'] or having to use $row[0]/$row[1], you can use $row['uID']/$row['fID']