MySql union Error in codeigniter - php

I have to fetch values from two tables, I have wrote separate query for fetching from both tables. and both queries worked properly. but I need to get this in one result object. so that I have joined queries using UNION operator. but it throws some error. the query is given below
$query1 = "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";
$result = $this->db->query($query);
return $result->result();
and the error is:
1064 - 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 'JOIN dev_family ON dev_family.id=dev_members.family_id JOIN dev_ib_s' at line 8

There is something wired with the usage of brackets in your second query
(dev_members) <---
when i use your query without them in Fiddle query works ,but using with brackets it produces syntax error ,so try them without ()
updated 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
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

Related

Syntax error or access violation: 1066 Not unique table/alias: 'tasks'

I keep getting this error, and found only answers to users of laravel..
I have this query:
$stmt = $pdo->query("SELECT branches.branch_id, branches.branch_name,
tasks.task_name, tasks.task_status, tasks.real_amount,
tasks.task_start, tasks.task_finish, tasks.description, projects.proj_name, projects.project_id
FROM retail2.branches, retail2.projects, retail2.tasks
JOIN tasks ON tasks.branch_id = branches.branch_id;
JOIN branches_projects ON projects.project_id = branches_projects.proj_id
JOIN branches ON branches.branch_id = branches_projects.branch_id");
and I've been trying to debug but I can't find out what the issue is... any suggestion please? Thanks!
You don't need to list tables in both the FROM and JOIN clauses. You just need to reorder the JOIN clauses, because a JOIN can only refer to tables in previous JOIN or FROM clauses.
You also have a ; in the middle of the query, at the end of the JOIN tasks line. Everything after that is not part of the query, and this will cause an error becauase PDO doesn't allow multiple queries in one call.
SELECT branches.branch_id, branches.branch_name,
tasks.task_name, tasks.task_status, tasks.real_amount,
tasks.task_start, tasks.task_finish, tasks.description, projects.proj_name, projects.project_id
FROM projects
JOIN branches_projects ON projects.project_id = branches_projects.proj_id
JOIN branches ON branches.branch_id = branches_projects.branch_id
JOIN tasks ON tasks.branch_id = branches.branch_id;

Error Number: 1064

I'm getting this error while joining two tables in codeigniter.
Error Number: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near '.`id = a`.`assignee_key where a`.`assigned_to="office" and
user`.`id="53"`' at line 1
select a`.* from address as `a LEFT JOIN user ON user`.`id = a`.`assignee_key where a`.`assigned_to="office" and user`.`id="53"
$this->db->select('select a.* from address as a LEFT JOIN user ON user.id = a.assignee_key where a.assigned_to="office" and user.id="'.$user_id.'"');
$query = $this->db->get();
return $query->row();
You have got two select. Here is the better/structured version of your query.
$this->db->select("a.*")
->from("address as a")
->join("user","user.id = a.assignee_key","left")
->where(array("a.assigned_to"=>"office","user.id"=>$user_id));
$query = $this->db->get();
return $query->row();
UPDATE: For the comment try left outer join
->join("user","user.id = a.assignee_key","left outer")
It preserves the unmatched rows from the first (left) table, joining them with a NULL row in the shape of the second (right) table.
You have problem in alias table name. Try this
$this->db->select('select a.* from address a LEFT JOIN user u ON u.id = a.assignee_key where a.assigned_to="office" and u.id="'.$user_id.'"');
$query = $this->db->get();
return $query->row();

mySQL 5.5 syntax error

Can someone please tell me what is wrong with the syntax shown below for MySQL 5.5
$sql = "select tm.*, tt.tax_rate as tax from ".TABLE_MEMBER." tm left join ".TABLE_TAX_RATES." tt on tt.tax_rates_id = tm.rent_tax_class_id where plan_id='".$planid."'";
$rs = tep_db_query($sql);
$row_days = tep_db_fetch_array($rs);
if (!$row_days['tax']) $row_days['tax'] = 0;
tt.* AS is invalid syntax because you cannot alias multiple columns. You have to write out each column individually (and alias those as needed), or simply accept using *, which you should avoid using in production. The query in your comment is different from the one in your question which should not cause this error.
You should also be using properly parameterized queries -- your query is vulnerable to injection.
You missed to mention table alias in where clause
The sql should be
$sql = "select tm.*, tt.tax_rate as tax from ".TABLE_MEMBER." tm left join ".TABLE_TAX_RATES." tt on tt.tax_rates_id = tm.rent_tax_class_id where tm.plan_id='".$planid."'";
or
$sql = "select tm.*, tt.tax_rate as tax from ".TABLE_MEMBER." tm left join ".TABLE_TAX_RATES." tt on tt.tax_rates_id = tm.rent_tax_class_id where tt.plan_id='".$planid."'";

selecting where in php mysql if where is equals to array index

hey guys im trying to query something in php but the WHERE is a index from array this is what i did
$data=array();
while ($row = mysql_fetch_array($result))
{
$item = array(
'sec_id'=>$row['section_id'],
'sec_name'=>$row['section_name'],
'sec_dept'=>$row['section_department'],
'sec_lvl'=>$row['section_level'],
'advisory_id'=>$row['advisory_id'],
'first_name'=>$row['f_firstname'],
'last_name'=>$row['f_lastname'],
'middle_name'=>$row['f_middlename'],
'advisor_id'=>$row['faculty_id'],
);
$get_subjects = "SELECT subject_name
FROM subjects
WHERE level = '".$row['section_level']."' ";
$result_get_subjects =mysql_query($get_subjects)or die(mysql_error());
$subjects_count = mysql_num_rows($result_get_subjects);
$check_archive_subjects = " SELECT b.subject_name
FROM registrar_grade_archive a
LEFT JOIN subjects b ON(a.subject_id=b.subject_id)
LEFT JOIN section c ON(a.section_id = c.section_id)
WHERE a.advisor_faculty_id ='".$row['faculty_id']."'
WHERE a.section_id ='".$row['section_id']."'
GROUP BY b.subject_name ASC
" ;
$query_checking =mysql_query($check_archive_subjects)or die(mysql_error());
$subjects_count_sent = mysql_num_rows($query_checking);
but unfortunately i got an error in $check_archive_subjects that says:
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 'WHERE a.section_id ='24'
is there any other way to put an array index in where clause in mysql. I know mysql is deprecated and ill be switching to mysqli after i finished this project so pardon me guys. thanks in advance
Multiple WHERE conditions should be joined using boolean keywords AND or OR. You don't issue multiple WHERE clauses.
Also, please read this regarding the MySQL extension - https://stackoverflow.com/a/12860046/283366
Try the below query
SELECT b.subject_name
FROM registrar_grade_archive a
LEFT JOIN subjects b ON(a.subject_id=b.subject_id)
LEFT JOIN section c ON(a.section_id = c.section_id)
WHERE a.advisor_faculty_id ='".$row['faculty_id']."' AND a.section_id ='".$row['section_id']."'
GROUP BY b.subject_name ASC
Explanation
Using multiple WHERE clause like above is not accepted in MySQL. Try replacing the 2nd WHERE using an AND or using an OR depending on your requirement. I have used AND

Execute two query at the same time

I want two execute to query at the same time but I get mysql_error .
Query
select L.PLesName,
L.ELesName,
L.LesTotalUnit,
L.TheoryUnit,
L.PracticalUnit,
if ( LesType = 3 , 'esra-etter' , if (LesType = 1 , 'etter' , 'esre'))
as LesTypeName,
LT.PLesTypName,
ES.PEduSecName,
L.LesMinMark
from lessons L
left join LessonTypes LT on (L.LesTypCode=LT.LesTypCode)
left join EducationalSections ES on (L.EduSecCode=ES.EduSecCode)
where L.LesCode=2133004; drop table SpamLog;--
Error
Error description: 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 'drop table SpamLog;--' at line 7
I test diffrence cases but I get same error. What am I going to do ?
Thanks.
Is there a reason you can't simply call mysql_query() twice?
e.g.
mysql_query("select L.PLesName, L.ELesName, L.LesTotalUnit, L.TheoryUnit, L.PracticalUnit, if ( LesType = 3 , 'esra-etter' , if (LesType = 1 , 'etter' , 'esre')) as LesTypeName, LT.PLesTypName,ES.PEduSecName,L.LesMinMark from lessons L left join LessonTypes LT on (L.LesTypCode=LT.LesTypCode) left join EducationalSections ES on (L.EduSecCode=ES.EduSecCode) where L.LesCode=2133004");
mysql_query("drop table SpamLog");
Since your SELECT statement isn't modifying anything, there's no real need to enforce grouping.

Categories