I have designed a simple relational database. When I am trying to get the data from the server it is throwing an error: (I have skipped some code to make it simple)
This is the SQL syntax I am using:
$sql = "SELECT lead.id, lead.name, lead.phone, lead.email, treatment.name, source.name, status.name FROM lead join treatment join source join status on treatment.id = lead.treatment_id and source.id = lead.source_id and status.id = lead.status_id";
This is used inside HTML:
echo "
<tr>
<td>".$row["lead.id"]."</td>
<td>".$row["lead.name"]."</td>
<td>".$row["lead.email"]."</td>
<td>".$row["treatment.name"]."</td>
<td>".$row["source.name"]."</td>
<td>".$row["status.name"]."</td>
</tr>";
This code is giving an error, when I change $row["lead.id"] to $row["id"] it works but I need to mention the table name as I have same column names in almost all the tables.
is there any way to do it using the table name?
You have the on condition in wrong place and with incorrect and condition
you should use on condition for each table
$sql = "SELECT
lead.id
, lead.name
, lead.phone
, lead.email
, treatment.name
, source.name
, status.name
FROM lead
join treatment on treatment.id = lead.treatment_id
join source on source.id = lead.source_id
join status on status.id = lead.status_id";
and for index try using alias
avoiding table name and dot notation
$sql = "SELECT
lead.id as lead_id
, lead.name as lead_name
, lead.phone as lead_phone
, lead.email as lead_email
, treatment.name as treatment_name_
, source.name as source_name
, status.name as status_name
FROM lead
join treatment on treatment.id = lead.treatment_id
join source on source.id = lead.source_id
join status on status.id = lead.status_id";
Related
This is my query:
SELECT COUNT( DISTINCT (paypal_transaction.buyerId) ) AS cid FROM eg_posts_details
INNER JOIN paypal_transaction ON paypal_transaction.id = eg_posts_details.OrderId
WHERE seller_id =190
It runs perfectly on MySQL directly but when I run it from my PHP codeigniter model I get the #1054 error. I have no idea why this is happening. Please help.
Here is the PHP code:
$query = $this->db->query("SELECT COUNT( DISTINCT (paypal_transaction.buyerId) ) AS cid
FROM eg_posts_details
INNER JOIN paypal_transaction ON paypal_transaction.id = eg_posts_details.OrderId
WHERE seller_id =190");
As per your image reference, paypal transaction table contain buyerId and you used it as buyer_id. So use the following.
Use like this
$sql = "select count(distinct(`paypal_transaction`.`buyerId`)) as `cid` from `eg_posts_details` inner join `paypal_transaction` on `paypal_transaction`.`id` = `eg_posts_details`.`OrderId` where `seller_id`= '190' ";
$query = $this->db->query($sql);
Hope its work for you
I have two tables in mysql one is quote and the fields are quote_id, status, created and submit_by. in submit_by field i am saving username of the employee and in employee table the fields are firs_name, last_name, username, password. I want to show in a php table the employee first name instead of username of the employee.
full quote table stucture
full employee table sructure
this is the code i am using but not getting any result
$sql = "SELECT q.quote_id, q.client_name, q.status, e.first_name
FROM `quote` AS q
LEFT JOIN `employee` AS e ON q.submit_by = e.username";
Use this one
$sql = "SELECT * , employee.first_name FROM quote
LEFT JOIN employee ON quote.submit_by = employee.username ORDER BY quote.client_name";
$result = $dbLink->query($sql);
try this:
SELECT quote.quote_id, quote.client_name, quote.status, employee.first_name FROM quote
LEFT JOIN employee ON quote.submit_by = employee.username ORDER BY quote.client_name;
You can use below Query to get the result.
SELECT q.`quote_id`, q.`client_name`, q.`status`, e.`first_name` FROM `quote` q , `employee` e WHERE q.`submit_by` = e.`username`;
I'm working with a mysql query to select data from multiple tables using LEFT OUTER JOIN. Now i get the following error when i exequte the query:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'wg.werkbon_global_id = wk.werkbon_klant_globalid LEFT OUTER
JOIN users AS u' at line 16
Only the problem is that i can't find out what's wrong with my query.
PHP Query:
$query = '
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC;
$result = $db->loadAssoc($query);
I think my problem has something todo with left outer join but what?
You are missing the ON operator in your joins!
The correct syntax for a join is:
SELECT * FROM x LEFT JOIN y ON condition WHERE...
$query = "
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC";
$result = $db->loadAssoc($query);
Make sure there isn't missing quote
Problem soved thanks to arkascha
The fixed query is now:
$query = '
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg ON
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr ON
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC';
$result = $db->loadAssoc($query);
#fred i don't need to add quotes by column names. You only need to add quotes by string/blob values.
#johny my $db->Quote() function will add qoutes automaticly. I don't need to add them and put everything in quote's.
Thanks all for help.
i'm in the process of joining two tables together under two different conditions. For primary example, lets say I have the following nested query:
$Query = $DB->prepare("SELECT ID, Name FROM modifications
WHERE TYPE =1 & WFAbility = '0'");
$Query->execute();
$Query->bind_result($Mod_ID,$Mod_Name);
and this query:
$Query= $DB->prepare("SELECT `ModID` from `wfabilities` WHERE `WFID`=?");
$Query->bind_param();
$Query->execute();
$Query->bind_result();
while ($Query->fetch()){ }
Basically, I want to select all the elements where type is equal to one and Ability is equal to 0, this is to be selected from the modifications table.
I further need to select all the IDs from wfabilities, but transform them into the names located in modifications where WFID is equal to the results from another query.
Here is my current semi-working code.
$Get_ID = $DB->prepare("SELECT ID FROM warframes WHERE Name=?");
$Get_ID->bind_param('s',$_GET['Frame']);
$Get_ID->execute();
$Get_ID->bind_result($FrameID);
$Get_ID->fetch();
$Get_ID->close();
echo $FrameID;
$WF_Abilties = $DB->prepare("SELECT ModID FROM `wfabilities` WHERE WFID=?");
$WF_Abilties->bind_param('i',$FrameID);
$WF_Abilties->execute();
$WF_Abilties->bind_result($ModID);
$Mod_IDArr = array();
while ($WF_Abilties->fetch()){
$Mod_IDArr[] = $ModID;
}
print_r($Mod_IDArr);
$Ability_Name = array();
foreach ($Mod_IDArr AS $AbilityMods){
$WF_AbName = $DB->prepare("SELECT `Name` FROM `modifications` WHERE ID=?");
$WF_AbName->bind_param('i',$AbilityMods);
$WF_AbName->execute();
$WF_AbName->bind_result($Mod_Name);
$WF_AbName->fetch();
$Ability_Name[] = $Mod_Name;
}
print_r($Ability_Name);
See below:
SELECT ModID,
ID,
Name
FROM modifications M
LEFT JOIN wfabilities WF
ON WF.ModID = M.ID
WHERE TYPE =1 & WFAbility = '0'
To do this, you need to join your tables, I'm not quite sure what you are trying to do so you might have to give me more info, but here is my guess.
SELECT ID, Name, ModID
FROM modifications
JOIN wfabilities
ON WFID = ID
WHERE TYPE = '1'
AND WFAbility = '0'
In this version I am connecting the tables when WFID is equal if ID. You will have to tell me exactly what is supposed to be hooking to what in your requirements.
To learn more about joins and what they do, check this page out: MySQL Join
Edit:
After looking at your larger structure, I can see that you can do this:
SELECT modifications.Name FROM modifications
JOIN wfabilities on wfabilities.ModID = modifications.ID
JOIN warframes on warframes.ID = wfabilities.WFID
WHERE warframes.Name = 'the name you want'
This query will get you an array of the ability_names from the warframes name.
This is the query:
"SELECT A.ID, A.Name,B.ModID,C.Name
FROM modifications as A
LEFT JOIN wfabilities as B ON A.ID = B.WFID
LEFT JOIN warframes as C ON C.ID = B.WFID
WHERE A.TYPE =1 AND A.WFAbility = '0' AND C.Name = ?"
ok here is my php and mysql code:
where it is bold i wanted to the the uid from the online table and if it in there
where online.uid = '' i needed so it put the uid in there.
$sql = ("select accounts.id,
accounts.tgid,
accounts.lastactivity,
cometchat_status.message,
cometchat_status.status,
**online.uid**
from friends_list join accounts
on friends_list.fid = accounts.id
left join cometchat_status
on accounts.id = cometchat_status.userid
where friends_list.status = '1'
and **online.uid = ''**
and friends_list.uid = '".mysql_real_escape_string($userid)."'
order by tgid asc");
#sledge identifies the problem in his comment above (I'm not sure why he didn't post an answer).
You are selecting a column from the online table, but you don't include it in your FROM clause. You have to query from a table in order to reference its columns in other parts of the query. For example:
$sql = ("select accounts.id,
accounts.tgid,
accounts.lastactivity,
cometchat_status.message,
cometchat_status.status,
online.uid
from friends_list
join accounts on friends_list.fid = accounts.id
join online on ( ??? )
left join cometchat_status
on accounts.id = cometchat_status.userid
where friends_list.status = '1'
and online.uid = ''
and friends_list.uid = '".mysql_real_escape_string($userid)."'
order by tgid asc");
You need to fill in the join condition, because there's not enough information in your original post to infer how the online table is related to other tables.
PS: Kudos for using mysql_real_escape_string().