mysql join 2 table to select one row fields with multiple rows - mysql

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

Related

Using LIKE with prepared statement

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.

Choosing from multiple columns by column name stored in a column

I want to select from a table like this
|id|col_name|col1|col2|col3|col4|col5|...|col100|
| 1|col1 | 142| 241| 333| 417| 713|...| 125|
| 2|col5 | 927| 72| 403| 104| 136|...| 739|
| 3|col100 | 358| 842| 150| 125| 174|...| 103|
Select from column specified by col_name field. Something like
SELECT id,valueof(col_name) val FROM table1
which returns
|id|val|
| 1|142|
| 2|136|
| 3|103|
If you are using PHP (or modify the logic accordingly), you can do something like-
$colNames = array(col1, col5, col100); // or SELECT col_name FROM
table_name and store it in $colNames
foreach ($colNames as $val) {
$query = "SELECT $val FROM table_name WHERE col_name={$val}";
//execute this and store its result one-by-one into another array.
}
as I'm not sure if it could be done with single query.

Slow mysql query full text search tag database

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);

Count Function - Codeigniter and MySQL

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();

mysql update table with values from another table?

I am trying to update my table 'supplier_stats' with the values from my other table 'supplier_change_request'.
My two tables look like the following:
Supplier_change_request
id | user_id | company_name | supplier_number
1 123 hewden V0001
Supplier_stats
Id | user_id | company_name | address | reference | supplier_number
1 123 pie n/a 12345 V0001
2 145 gates n/a 12345 V0002
Here is my MySQL:
$reference = '12345'
$query = "UPDATE supplier_stats
SET supplier_stats.company_name = (
SELECT supplier_change_request.company_name
FROM supplier_change_request
WHERE supplier_change_request.reference = '$reference' AND supplier_change_request.supplier_number = supplier_stats.supplier_number";
mysql_select_db('hewden1');
$retval = mysql_query( $query, $conn )
by my calculation this should be setting the value of company_name where supplier_number is 'V0001' in my table 'supplier_stats' to 'hewden'. However the company_name is not being updated.
Can someone please show me where I am going wrong? Thank you in advance
I think the syntax is a bit off in your query and that it should look like this (just the SQL, adapt to PHP as needed):
UPDATE supplier_stats ss
JOIN supplier_change_request scr ON scr.supplier_number = ss.supplier_number
SET ss.company_name = scr.company_name
WHERE ss.reference = '$reference'
The column reference pointed to the supplier_change_request in your sample query, but to supplier_stats in your sample data - I assumed the sample data was correct; change if not.
This query should change the company_name in supplier_stats from pie to hewden.