I have a table as below:
table name: brand
id | brand
1 | UNIQLO
2 | PDI
3 | PDI
4 | H&M
5 | UNIQLO
The result that I need is:
PDI x 2
UNIQLO x 2
H&M x 1
I have tried this :
$this->db->select('brand, count(*) as TOTAL');
$this->db->from('brand');
$this->db->group_by('id');
$query = $this->db->get();
return $query->result();
But my output is uniqlopdiuniqlopdiH&Muniqlo.
Anyone can solve my problem?
Michael Berkowski is right
It should be group by brand.
Your query will be like this
$this->db->select('brand, count(*) as TOTAL');
$this->db->from('brand');
$this->db->group_by('brand');
$query = $this->db->get();
return $query->result();
Related
I have a statement I wish to execute to find if a column containing a string contains a certain value.
+----+------+
| id | st |
+----+------+
| 0 | 2183 |
| 1 | 5820 |
| 2 | 2984 |
| ...| ... |
+----+------+
Say I wish to find all rows where st contains a 1, I would use these where conditions:
WHERE st LIKE "%1%"
OR st LIKE "1%"
OR st LIKE "1"
OR st LIKE "%1"
But how do I do this in a prepared statement?
$ps = $db->prepare("
SELECT id
FROM table
WHERE st LIKE "%:a%"
OR st LIKE ":a%"
OR st LIKE ":a"
OR st LIKE "%:a"
");
$ps->execute(array(
':a' => $var
));
This doesn't work evidently.
The % sign must be part of $var not part of the prepared Statement. also you only need %:a% it include all other parts of your where clause
$ps = $db->prepare("
SELECT id
FROM table
WHERE st LIKE :a
");
$ps->execute(array(
':a' => "%".$var."%"
));
ps = $db->prepare("
SELECT id
FROM table
WHERE st LIKE :a");
$ps->execute(array(
':a' => "%".$var."%"
));
Try above code.
And one more thing if you require rows which contains 1 in it,then there is no requirement of 4 like condition you only can achieve using '%1%'.
Hope this will help.
+------+------+
| col1 | col2 |
+------+------+
| 1 | 0 |
| 1 | 3 |
| 1 | 2 |
+------+------+
How can I prevent mysql from returning any row with col1 = 1 when its col2 has 0 even in other rows? So in this case, I don't want to get any row because first row has col2 = 0.
EDIT
If this is in the first row col1 = 1 AND col2 = 1instead of col1 = 1 AND col2 = 0
Then the result should be the three rows
Or else there should be no results
$array = array('col1' => '1','col2 !=' => '0');
$this->db->select('col1,col2');
$this->db->from('table');
$this->db->where($array);
$query = $this->db->get();
I think this would work for you..
or else if you want all row with col2!=0 then change array to
$array = array('col2 !=' => '0');
or you can do it simply mysql way as #Shihas suggested following way
$where = "col1='1' AND col2!='0'";
$this->db->where($where);
Hope this helps.
Revised my answer as per your requirement..
$sql = mysqli_query($this->db,"SELECT col1,col2 from table where col2=0") or die(mysqli_error($db));
if(mysqli_num_rows($sql)>0){
$error = array("status"=>"Failed","msg"=>"There is data exist with col2 as 0");
$this->response($this->json($error),400);
}
else{
//put your condition here
$sql = mysqli_query($this->db,"SELECT col1,col2 from table where col1=1 col2!=0") or die(mysqli_error($db));
}
you can put your query in else part,whatever you want to get executed
Finally I have discovered the answer to my own question. The trick is to use the group by and having clause
SELECT col1 FROM table WHERE col1 = 1 GROUP BY col2 HAVING col2 != 0
Thank you very much for those who tried to answer.
Need some help with a query.
In the database are ~700k of images, each image has it's own tag(s).
I want to be able to search images by it's tags with a full text search query.
The query does exactly what i want but it's very slow.
Can some please help me speed to speed it up, or create an other one.
There a indexes on al the id-fields and the name field in de 'image_tag' table.
SELECT
image.*
FROM image
INNER JOIN (SELECT image_to_tag.image_id,
GROUP_CONCAT(image_tag.`name`) AS tags
FROM image_to_tag
INNER JOIN image_tag
ON (image_tag.id = image_to_tag.image_tag_id)
GROUP BY image_to_tag.image_id) t ON t.image_id = image.id
WHERE MATCH (t.tags) AGAINST ('+justin* +backgrounds*' IN BOOLEAN MODE)
Table: image
id | filename
1 | image1.jpg
2 | image2.jpg
3 | image3.jpg
Table: image_to_tag
image_id | image_tag_id
1 | 1
1 | 2
2 | 3
2 | 4
Table: image_tag
id | name
1 | justin bieber
2 | backgrounds
3 | justin timberlake
4 | other backgrounds
If i search for "justin background" i want to find image 1 and 2.
If i search for "justin bieber background" i want to find image 1.
Please try this and let me know if with this could be a little faster.
select id,filename from image
where id in(
select image_id from image_to_tag a
inner join image_tag b
on b.id = a.image_tag_id and b.name like '%justin%'
)
You can boost up the performance by splitting your query into three parts. It will give your results in milliseconds :
$tagids = '';
$res = $mysqli->query("SELECT group_concat(id) as tagids FROM image_tag WHERE MATCH (name) AGAINST ('+justin* +backgrounds*' IN BOOLEAN MODE");
if($row = $res->fetch_object()){
$tagids = $row->tagids;
}
$res->close();
$imageids = '';
$res = $mysqli->query("SELECT group_concat(image_id) as imageids FROM image_to_tag WHERE image_tag_id in($tagids)");
if($row = $res->fetch_object()){
$imageids = $row->imageids;
}
$res->close();
$imgarr = array();
$res = $mysqli->query("SELECT id,filename FROM image WHERE id in($imageids)");
if($row = $res->fetch_object()){
array_push($imgarr,$row);
}
$res->close();
echo json_encode($imgarr);
I'm developing an application, Where in I've defined following tables.
storytags
id cover_title user_id
1 love happens two times? 1
2 revolution 2020 2
3 wings of fire 3
4 night at the call centre 4
storytag_invitations
id storytag_id user_id
1 1 1
2 2 2
3 3 3
4 4 4
users
id name
1 suhas
2 sangu
3 praveen
4 sangamesh
I want to fetch the storytags where storytag_invitations.user_id != storytags.user_id and storytag_invitations.storytag_id != storytags.id for the user 3
I've tried the following query
select storytags.cover_title
from storytag_invitations
join storytags
on storytags.id != storytag_invitations.storytag_id and storytags.user_id != storytag_invitations.user_id
where storytag_invitations.user_id = 3
But I'm getting duplicate rows. Please suggest some solutions. Its been two days I'm trying this. The work will be more appreciated.
Your sql works for me when I use from:
select s.cover_title
from storytag_invitations si, storytags s
where s.id != si.storytag_id
and s.user_id != si.user_id
and si.user_id = 3
You can check it out here: http://sqlfiddle.com/#!4/ecd77/4
Try if it works for you:
$sql = "select storytags.cover_title from storytags, storytag_invitations where ( storytags.id != storytag_invitations.storytag_id and storytags.user_id != storytag_invitations.user_id ) and storytag_invitations.user_id = 3";
$rs = $this->db->query($sql);
//if you are using codeigniter then try this
$this->db->select("table1.column1,table1.column2,table2.column");
$this->db->join("table2","table1.column = table2.column");
$resultset=$this->db->get();
I need to join two table as follows - table 2 'value' on table 1 'price_1', 'price_2' & 'price_3' so I can output price label instead of the price value. Not sure how approach this in codeigniter. Do I use join the then nested select?:
table 1
id | price_1 | price_2 | price_3
1 | 6 | 5 | 4
Table 2
id | label | value
1 | £6.50 | 6
2 | £2.50 | 5
3 | £4.00 | 4
Any pointers would be appreciated.
You can first make a select from table 1. Then:-
$tags = array();
foreach($record_from_table1 as $record)
{
$tags[] = $record['price1'];
$tags[] = $record['price2'];
$tags[] = $record['price3'];
}
Then make array_unique($tags)
Make select query from table 2 and get corresponding label values and echo them.
thanks for pointers, but did it this way, based on Codeigniter Join with Multiple Conditions
$this->db->select('p1.label as pa_1, p2.label as pa_2, p3.label as pa_3, p4.label as pa_4, p5.label as pa_5');
$this->db->from('table1');
$this->db->join('table2 as p1', 'p1.value = price_1', 'left');
$this->db->join('table2 as p2', 'p2.value = price_2', 'left');
$this->db->join('table2 as p3', 'p3.value = price_3', 'left');
$query = $this->db->get();
Thanks, Dan