$query=mysql_query("SELECTcom_jobcard.job_card_num,process.process_name,employee.emp_name,employee.emp_code ,SUM(worked_qty) FROMcom_jobcard
INNER JOIN timer_completed ON timer_completed.job_card_id = com_jobcard.id
INNER JOIN process ON process.id = timer_completed.process_id
INNER JOIN employee ON employee.id = timer_completed.employee_id
AND job_card_id = ( SELECT id FROM com_jobcard WHERE job_card_num = 'JCID-1271742')
GROUP BY process.process_name,employee.emp_name ");
$query1 = mysql_query("SELECT com_jobcard.job_card_num,process.process_name,employee.emp_name,employee.emp_code,SUM(qc.rework_qty),qc.report_date,qc.id FROM qc
INNER JOIN com_jobcard ON qc.job_card_id = com_jobcard.id
INNER JOIN process ON process.id = qc.process_id
INNER JOIN qc_employees ON qc_employees.qc_id = qc.id
INNER JOIN employee ON employee.id = qc_employees.employee_id
AND qc_employees.qc_id = (SELECT qc.id
FROM qc
INNER JOIN com_jobcard ON qc.job_card_id = com_jobcard.id
AND com_jobcard.job_card_num = 'JCID-1271742')
GROUP BY process.process_name,employee.emp_name");
while ($row = mysql_fetch_array($query)) {
$row1 = mysql_fetch_array($query1);
if( ($row[1] == $row1[1]) && ($row[3] == $row1[3] ))
{
echo $row1[0] .$row1[1] .$row1[2] .$row1[3] .$row1[4];
}
}
Dear friends I am executing 2 queries simultaneously but I would like to compare both the result sets when $row[1] == $row1[1]) && ($row[3] == $row1[3] the condition is satisfied then it has to return the result but as if now it returns nothing could any one suggest me how to compare the result set of both the queries , Instead of using IF I have also used While statement , though I didn't recieve the result .
Please help me!
Thank you!
Use it like this
$row1 = mysql_fetch_array($query1);
while ($row = mysql_fetch_array($query)) {
while($row1) {
if( ($row['process_name'] == $row1['process_name']) && ($row['emp_name'] == $row1['emp_name'] ))
{
echo $row1['job_card_num'] .$row1['process_name'] .$row1['emp_name'] .$row1['emp_code'] .$row1['sum_qty'];
}
}
}
Because you have used $row[1] $row1[1] which has no value.
I ahev used alias vakue for SUM(qc.rework_qty) in your query as sum_qty
Related
I'm using Slim with PDO with MySql for return a specific list of matches available in my database. My query is this:
SELECT m.*,
t.name AS home_team_name,
t2.name AS away_team_name
FROM `match` m
LEFT JOIN team t ON m.home_team_id = t.id
LEFT JOIN team t2 ON m.away_team_id = t2.id
WHERE (home_team_id = 117 OR away_team_id = 117) AND round_id = 488
if I execute this query I'll get a list of matches:
but inside the API developed with Slim I get an empty array. This is the method structure:
$app->get('/match/get_matches_by_team/{round_id}/{team_id}/{type}', function (Request $request, Response $response, array $args)
{
$query = "SELECT m.*,
t.name AS home_team_name,
t2.name AS away_team_name
FROM `match` m
LEFT JOIN team t ON m.home_team_id = t.id
LEFT JOIN team t2 ON m.away_team_id = t2.id
WHERE ";
switch($args["type"])
{
case "home":
$query .= "home_team_id = :team_id AND ";
break;
case "away":
$query .= "away_team_id = :team_id AND ";
break;
default:
$query .= "(home_team_id = :team_id OR away_team_id = :team_id) AND ";
break;
}
$query .= "round_id = :round_id";
$sql = $this->db->prepare($query);
$sql->bindParam("team_id", $args["team_id"]);
$sql->bindParam("round_id", $args["round_id"]);
$sql->execute();
$result = $sql->fetchAll();
return $response->withJson($result);
});
what I did wrong?
Thanks in advance for any help.
UPDATE
If I do echo $query; return; I'll get:
SELECT m.*,
t.name AS home_team_name,
t2.name AS away_team_name
FROM `match` m
LEFT JOIN team t ON m.home_team_id = t.id
LEFT JOIN team t2 ON m.away_team_id = t2.id
WHERE away_team_id = :team_id AND round_id = :round_id
supposing to pass away, if instead I pass all I'll get:
SELECT m.*,
t.name AS home_team_name,
t2.name AS away_team_name
FROM `match` m
LEFT JOIN team t ON m.home_team_id = t.id
LEFT JOIN team t2 ON m.away_team_id = t2.id
WHERE (home_team_id = :team_id OR away_team_id = :team_id) AND round_id = :round_id
UPDATE 2
Method updated with proposed hints
$app->get('/match/get_matches_by_team
/{round_id}/{team_id}/{type}', function (Request $request, Response $response, array $args)
{
$query = "SELECT m.*,
t.name AS home_team_name,
t2.name AS away_team_name
FROM `match` m
LEFT JOIN team t ON m.home_team_id = t.id
LEFT JOIN team t2 ON m.away_team_id = t2.id
WHERE ";
switch($args["type"])
{
case "home":
$query .= "home_team_id = :home_team_id
AND ";
break;
case "away":
$query .= "away_team_id = :away_team_id AND ";
break;
default:
$query .= "(home_team_id = :home_team_id OR away_team_id = :away_team_id) AND ";
break;
}
$query .= "round_id = :round_id";
$sql = $this->db->prepare($query);
$sql->bindParam("home_team_id", $args["team_id"]);
$sql->bindParam("away_team_id", $args["team_id"]);
$sql->bindParam("round_id", $args["round_id"]);
$sql->execute();
$result = $sql->fetchAll();
return $response->withJson($result);
});
yours:
$sql->bindParam("team_id", $args["team_id"]);
$sql->bindParam("round_id", $args["round_id"]);
try this, the params might be needed to be formatted differently
$sql->bindParam(":team_id", $args["team_id"], PDO::PARAM_INT);
$sql->bindParam(":round_id", $args["round_id"], PDO::PARAM_INT);
or
$sql->bindParam(":team_id", $args["team_id"]);
$sql->bindParam(":round_id", $args["round_id"]);
When the default switch is selected, you are trying to bind a value with the same parameter marker (in your case :team_id) twice. In order for this to work you have to turn on emulation mode in PDO.
http://www.php.net/manual/en/pdo.prepare.php
You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.
I have this query and when I add a OR, return multiples rows, how I can fix this? I need another left join or Right join?
SELECT `images`.`id` as `id_image`, `images_lang`.`title` as `title`, `images_lang`.`autor` as `author`, `media_lang`.`file`, `category_lang`.`title` as `category`
FROM `images`
JOIN `images_lang` ON `images_lang`.`id_images` = `images`.`id`
JOIN `category_lang` ON `images`.`id_category` = `category_lang`.`id_category`
JOIN `media` ON `images`.`id` = `media`.`id_item`
JOIN `media_lang` ON `media`.`id` = `media_lang`.`id_media`
JOIN `relation` ON `relation`.`from_id` = `images`.`id`
JOIN `tag` ON `tag`.`id` = `relation`.`to_id`
JOIN `tag_lang` ON `tag`.`id` = `tag_lang`.`id_lang`
WHERE `media`.`table` = 'images'
AND `media_lang`.`id_lang` = '1'
AND `images_lang`.`id_lang` = '1'
AND `category_lang`.`id_lang` = '1'
AND `images`.`active` = 1
AND `relation`.`type` = 2
AND `tag_lang`.`id_lang` = '1'
AND `tag_lang`.`slug` = 'pa-am-oli'
OR `tag_lang`.`slug` = 'playa'
LIMIT 9
and the code in codeigniter, but I donĀ“t how add the or after the and ($keywords is an array for filter the elements)
$this->db->select('images.id as id_image, images_lang.title as title, images_lang.autor as author, media_lang.file, category_lang.title as category');
$this->db->from($this->table);
$this->db->join($this->table.'_lang',$this->table.'_lang.id_images = '.$this->table.'.id');
$this->db->join('category_lang',$this->table.'.id_category = category_lang.id_category');
$this->db->join('media',$this->table.'.id = media.id_item');
$this->db->join('media_lang','media.id = media_lang.id_media');
$this->db->where('media.table',$this->table);
$this->db->where('media_lang.id_lang',$id_lang);
$this->db->where($this->table.'_lang.id_lang', $id_lang);
$this->db->where('category_lang.id_lang', $id_lang);
$this->db->where('images.active', 1);
if($category){
$this->db->where('category_lang.title', $category);
}
if($keywords){
$this->db->join('relation', 'relation.from_id = '.$this->table.'.id');
$this->db->join('tag', 'tag.id = relation.to_id');
$this->db->join('tag_lang', 'tag.id = tag_lang.id_lang');
$this->db->where('relation.type', _IMAGES_2_TAGS_);
$this->db->where('tag_lang.id_lang', $id_lang);
foreach($keywords as $tag){
$this->db->or_where('tag_lang.slug', $tag);
}
}
if($from || $limit)
{
$this->db->limit((int)$limit, (int)$from);
}
$query = $this->db->get();
return $query->result_array();
I solved the query adding this (and a Select Distinct)
AND (`tag_lang`.`slug` = 'playa'
OR `tag_lang`.`slug` = 'pa-am-oli')
How I can add this in codeigniter?
I don't get what you need exactly, but if you want have just one line as a return by filtering using OR
In your case you should do the following:
WHERE (`media`.`table` = 'images' OR `tag_lang`.`slug` = 'playa')
AND (`media_lang`.`id_lang` = '1' OR `tag_lang`.`slug` = 'playa')
and so on, I hope that helps you and good luck!
Astute use of parentheses would solve your problem, but this is easier to read...
SELECT i.id id_image
, il.title
, il.autor author
, ml.file
, cl.title category
FROM images i
JOIN images_lang il
ON il.id_images = i.id
JOIN category_lang cl
ON cl.id_category = i.id_category
JOIN media m
ON m.id_item = i.id
JOIN media_lang ml
ON ml.id_media = m.id
JOIN relation r
ON r.from_id = i.id
JOIN tag t
ON t.id = r.to_id
JOIN tag_lang tl
ON tl.id_lang = t.id
WHERE m.table = 'images'
AND ml.id_lang = 1
AND il.id_lang = 1
AND cl.id_lang = 1
AND i.active = 1
AND r.type = 2
AND tl.id_lang = 1
AND tl.slug IN('pa-am-oli','playa')
LIMIT 9
... however, note that LIMIT without ORDER BY is fairly meaningless.
I have two separate SQL queries that I would like to combine to a single one if possible.
Query #1 yields all entries from a table in random order
Query #2 will afterwards check whether or not the result can be used
How can I achieve this in a single step directly in SQL?
Code:
// start with a query for all of the photos, returned in random order
$query = "
SELECT DISTINCT m.mediaID
, m.description
, m.path
, m.alwayson
, m.usecollfolder
, m.mediatypeID
FROM $media_table m
WHERE m.mediatypeID = 'photos'
ORDER BY RAND();
";
$result = mysql_query($query) or die ("$text[cannotexecutequery]: $query");
while( $imgrow = mysql_fetch_assoc( $result ) ) {
// if the picture is alwayson or we are allowing living to be displayed,
// we don't need to bother
// with any further checking
if ($imgrow[alwayson] || $allow_living_db ) {
break;
// otherwise, let's check for living
} else {
// this query will return rows of personIDs on the photo that are living
$query = "
SELECT l.personID
FROM $medialinks_table l
JOIN $people_table p ON l.personID = p.personID
WHERE l.mediaID = $imgrow[mediaID]
AND p.living = 1
";
$presult = mysql_query($query) or die ("$text[cannotexecutequery]: $query");
$rows = mysql_num_rows( $presult );
mysql_free_result( $presult );
// if no rows are returned, there are no living on the photo, so let's display it
if ($rows == 0) {
break;
}
}
SELECT DISTINCT m.mediaID
, m.description
, m.path
, m.alwayson
, m.usecollfolder
, m.mediatypeID
, l.personID
FROM $media_table m
JOIN $medialinks_table l
ON l.mediaID = m.mediaID
JOIN $people_table p
ON l.personID = p.personID
WHERE m.mediatypeID = 'photos'
AND p.living = 1
ORDER
BY RAND();
I have a left join select statement that I want to select specific columns based on the value of a variable that has either a '0' or a '1'. I thought I could implement this using CASE, but I don't seem to be having any luck.
If $variable contains a '0' I want my select statement to retrieve only users.user, table2.total1 and table2.total2, but if the $variable contains a '1' I want to select only users.user and table2.total.
$value = '1';
$conn->query(
"select users.user, table2.total, table2.total1, table2.total2
FROM users
LEFT JOIN table2 on users.user = table2.total AND $table2.date = CURDATE()
WHERE users.user = 'marketing' OR users.user = 'sales'
");
Does anyone have any ideas? Thanks.
You don't need to do this with sql, if in fact the query differ only by the select clause you could use a conditional and a variable:
$value = '1';
if($variable == '1') {
$select = "select users.user, table2.total1, table2.total2 ";
}
else {
$select = "select users.user, table2.total ";
}
$conn->query(
$select . "
FROM users
LEFT JOIN table2 on users.user = table2.total AND $table2.date = CURDATE()
WHERE users.user = 'marketing' OR users.user = 'sales'
");
I have the following code and was wondering how I could simplify it into one MYSQL statement
$sql = "SELECT sesionID
FROM `juegos_sesiones`
WHERE `usuarioID` = ".$_SESSION['MM_UserID'];
$query = mysql_query($sql, $cnx) or die(mysql_error());
//$row_rsPaises = mysql_fetch_assoc($rsPaises);
$exercises = mysql_num_rows($query);
echo $exercises;
//find total points
while( $row = mysql_fetch_array($query)){
$sql2 = 'SELECT correct_answers FROM `juegos_sesiones_detalle` WHERE `sesionID` = '.$row['sesionID'];
$query2 = mysql_query($sql2, $cnx) or die(mysql_error());
$details2 = mysql_fetch_assoc($query2);
$counter = $counter + $details2['correct_answers'];
}
echo '<br />'.$counter;
SELECT sum(correct_answers) as total_correct_answers
FROM `juegos_sesiones_detalle` d
INNER JOIN `juegos_sesiones` s on s.`sesionID` = d.`sesionID`
WHERE `usuarioID` = $_SESSION['MM_UserID']
You can read about joins here
You can write single query by using INNER JOIN as:
SELECT correct_answers
FROM juegos_sesiones_detalle a
INNER JOIN juegos_sesiones b
ON a.sesionID = b.usuarioID;
Try this :
$sql = "SELECT correct_answers FROM `juegos_sesiones_detalle` jsd
JOIN `juegos_sesiones` js
ON jsd.sesionID = js.`sesionID`
WHERE js.usuarioID = ".$_SESSION['MM_UserID'];