Remove values in comma separated list from database on CodeIgniter - php

Edit :
I give another example,
products_table
| ID | product_name | images |
|---------------------------------------|
| 1 | Phone | img1.jpg,img2.jpg
| 2 | Canon | img1.jpg,img2.jpg,img3.jpg
| 3 | Cocac | img1.jpg,img2.jpg,img3.jpg,img4.jpg
| 4 | Lamps | img1.jpg,img2.jpg,img3.jpg,img4.jpg
Lats say I remove image "img3.jpg" from Lamps product without remove other images. What is the SQL call to this?

$this->db->query("UPDATE products_table SET images=replace(images, 'img3.jpg,',''),images=replace(images, ',img3.jpg','') WHERE product_name='Lamps'");

Related

How to get result from two different table with one single input

I have two different tables with different column name
Table 1 users
+-----+----------+---------+---------+------------+
| id | name | username| image | email |
+-----+----------+---------+---------+------------+
| 1 | John1 | exmple1 | img1 |a#gmail.com |
| 2 | John2 | exmple2 | img2 |b#gmail.com |
| 3 | John3 | exmple3 | img3 |c#gmail.com |
| 4 | John4 | exmple4 | img4 |d#gmail.com |
+-----+----------+---------+---------+------------+
Table 2nd Company
+-----+----------+------------+---------=------+-----------+
| id |company_name | username| description | founded |
+-----+-------------+---------+----------------+-----------|
| 1 | john1 | exmple1 |description1 | 2016 |
| 2 |CompanyName2 | exmple2 |description2 | 2016 |
| 3 |CompanyName3 | exmple3 |description3 | 2016 |
| 4 |CompanyName4 | exmple4 |description4 | 2016 |
+-----+-------------+---------+---------=------+-----------+
Now whenever a user type any input in search bar an ajax request is made to php file which checks for if that name of user exist in database or not
so for example if user type input john then a query should run which will check john in users table and company table & if there is a user name john & if there is a company named john it should fetch both the result.
how can i achieve this, i tried using UNION in my query but it said columns are different
Currently this is my query
$go = mysqli_query($connecDB, "SELECT name, img,username FROM users WHERE name LIKE '$q%' LIMIT 0,10");
Now people might be thinking what i really want!
i want a single query that will check for input in both table & fetch their details
You can use union
select id, name ,username, image, email, null, null
from users
where name LIKE concat('$q', '%')
union all
select id, company_name as name ,username, null, null, description, founded
from Company
where name LIKE concat('$q, '%')
order by name limit 10

multiple or single selection

i have two tables and column name are as :
Table 1
user | food | color | bike | car
Table 2
user | mobile | laptop
Now i want to get result by select single or multiple value.
For example, if i want select user which have bike and laptop . then i can get result it by query but for this all fields i have to use many condition . i have used if else where. and i also want to refine select with current selection . so what should i use ? Please help my previous question was same but i did not asked perfectly. so asked again. Thank You.
You can use multiple tables in your single SQL query. The act of joining in MySQL refers to smashing two or more tables into a single table.
You can use JOINS in SELECT, UPDATE and DELETE statements to join MySQL tables.
Example
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran | 20 |
| mahnaz | NULL |
| Jen | NULL |
| Gill | 20 |
| John Poul | 1 |
| Sanjay | 1 |
+-----------------+----------------+
SELECT * from tutorials_tbl;
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
| 1 | Learn PHP | John Poul | 2007-05-24 |
| 2 | Learn MySQL | Abdul S | 2007-05-24 |
| 3 | JAVA Tutorial | Sanjay | 2007-05-06 |
+-------------+----------------+-----------------+-----------------+
SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
-> FROM tutorials_tbl a, tcount_tbl b
-> WHERE a.tutorial_author = b.tutorial_author;
+-------------+-----------------+----------------+
| tutorial_id | tutorial_author | tutorial_count |
+-------------+-----------------+----------------+
| 1 | John Poul | 1 |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
http://www.tutorialspoint.com/mysql/mysql-using-joins.htm

how to merge multiple rows with same id mysql

I'm new posting here but the community have been my best resource on my projects so far.
I'm a dumb/dummy Mysql "wanna be" and I'm in the middle of a project that is making me go mad.
I have a table from wordpress plugin buddypress that pairs meta_key and meta_values in order to create something akin to a taxonomy. My duty is to use these paired values to implement an advanced group search. Here is the original table:
--------------------------------------------
id | group_id | meta_key | meta_value
--------------------------------------------
1 | 1 | time-zone | Kwajalein
2 | 1 | playstyle | hardcore
3 | 1 | recruiting-status | Open
4 | 1 | ilvl | 115
5 | 1 | main-raid | Final Coil of Bahamut
6 | 1 | voicechat | fc.teamspeak3.com
etc....
Using a view I managed to create a more friendly searchable table for begginers :
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 | | |
1 |Kwajalein | |
1 | | hardcore |
1 | | |
1 | | | Final Coil of Bahamut
1 | | |
And here is the view code:
SELECT distinct
group_id AS 'gid',
IF(meta_key='recruiting-status',meta_value,'') AS 'Recruitment',
IF(meta_key='server',meta_value,'') AS 'server',
IF(meta_key='time-zone',meta_value,'') AS 'tzone',
IF(meta_key='main-raid',meta_value,'') AS 'raid',
IF(meta_key='raid-days',meta_value,'') AS 'days',
IF(meta_key='playstyle',meta_value,'') AS 'playstyle',
IF(meta_key='raid-progression',meta_value,'') AS 'progression',
IF(meta_key='raid-time',meta_value,'') AS 'time',
IF(meta_key='tanker-spot',meta_value,'') AS 'tank',
IF(meta_key='healer-spot',meta_value,'') AS 'healer',
IF(meta_key='melee-dps-spot',meta_value,'') AS 'melee',
IF(meta_key='ranged-dps-spot',meta_value,'') AS 'ranged',
IF(meta_key='magic-dps-spot',meta_value,'') AS 'magic',
IF(meta_key='ilvl',meta_value,'') AS 'ilvl',
IF(meta_key='voicechat',meta_value,'') AS 'voice',
IF(meta_key='voicechatpass',meta_value,'') AS 'voicep',
FROM wpstatic_bp_groups_groupmeta
The point is, I need to merge that result (view) so all the group_id=1 or 2 or 3, etc stand in one single row, like this:
gid| time-zone| playstyle | main-raid
--------------------------------------------
1 |Kwajalein | hardcore | Final Coil of Bahamut
2 |SaoPaulo | regular | Second Coil of Bahamut
etc
Can anyone help me there?
Just surround your IFs in a MAX, or another aggregate function that will capture the non-empty strings (e.g., GROUP_CONCAT), and add a GROUP BY group_id add the end. For example,
SELECT
group_id AS gid,
MAX(IF(meta_key='recruiting-status',meta_value,'')) AS 'Recruitment',
MAX(IF(meta_key='server',meta_value,'')) AS 'server',
...
FROM wpstatic_bp_groups_groupmeta
GROUP BY group_id

How to count number of rows with the same column data and display to table?

I have 2 tables, the 'department' and 'document'.
Table department
| doc_id | dept_name |
----------------------------------
| 1 | Information Technology|
| 2 | Software Development |
| 3 | Human Resource |
| 4 | Accounting |
| 5 | Support |
Table document
| doc_id | doc_name | author | description | department |
----------------------------------------------------------------------------
| 1 | Maps | User1 | sample | Information Technology |
| 2 | Audits | User3 | sample | Software Development |
| 3 | Image | User1 | sample | Information Technology |
| 4 | Papers | User4 | sample | Human Resource |
| 5 | Print Screen| User1 | sample | Software Development |
| 6 | Transaction | User3 | sample | Accounting |
| 7 | Graph | User1 | sample | Support |
| 8 | Excel | User1 | sample | Information Technology |
Now, I want to display the table with two columns: department and total_doc.
Output:
| department |total_doc|
-----------------------------------
| Information Technology| 3 |
| Software Development | 2 |
| Human Resource | 1 |
| Accounting | 1 |
| Support | 1 |
I want to display the total document inside the department and arrange them in ascending order.
Here's my query.(not sure)
SELECT department, count(doc_name) as 'total_doc' FROM tbl_document GROUP BY doc_name
I'm using MVC pattern in Codeigniter.
$this->db->select("department, count(doc_name) as 'total_doc'");
$this->db->from('document');
$this->db->group_by('doc_name');
Also, How can I display this in table? like using foreach in html?
You need to do group by with department not with doc_name.
$this->db->select("department, count(doc_name) as 'total_doc'");
$this->db->from('document');
$this->db->group_by('department');
$result = $this->db->get()->result();
Hope This will help you.
foreach ($result as $row)
{
echo $row->department."----".$row->total_doc;
}
here you go
SELECT dept_name,COUNT(td.department) FROM department d
LEFT JOIN tdocument td ON td.`department`=d.`dept_name`
GROUP BY td.`department` ORDER BY COUNT(td.`department`) DESC;
You want one line per department. IN SQL words: You want to group by department.
select department, count(*) as total_doc from document group by department;
(BTW: don't use single quotes for column aliases.)

Trying to insert one column from table1 into existing column in table2?

I have two tables : This is the clients table.
+--------+---------+--------+------------+--------------+------------+
| id | name | debt | phone | address | products |
+--------+---------+--------+------------+--------------+------------+
| 1 | person1 | 55.54 | 8187654545 | Planet Earth | (none yet) |
| 1 | person2 | 55.64 | 8184256595 | Planet Mars | (none yet) |
+--------+---------+--------+------------+--------------+------------+
This is the products table:
+-----+----------+----------+----------+----------------+-------+
| id | category | name | type | size_in_ounces | price |
+-----+----------+----------+----------+----------------+-------+
| 1 | liquids | product1 | original | 2.7 | 1.99 |
| 1 | liquids | product2 | original | 7.7 | 5.99 |
| 1 | liquids | product3 | original | 10 | 7.99 |
+-----+----------+----------+----------+----------------+-------+
I am trying to add the name column from a row in the products table into the products column in the clients table. Basically what I am trying to accomplish is, on query, this to show in the clients table, and if possible for the clients debt column to increase in price accordingly.
+--------+---------+--------+------------+--------------+------------+
| id | name | debt | phone | address | products |
+--------+---------+--------+------------+--------------+------------+
| 1 | person1 | 63.49 | 8187654545 | Planet Earth | product3 |
| 1 | person2 | 57.63 | 8184256595 | Planet Mars | product1 |
+--------+---------+--------+------------+--------------+------------+
I tried to do this: but SQL gives me an error. I know the following code is wrong but it can give you an idea of what I'm trying to accomplish.
INSERT INTO `clients` (products) WHERE `name` = "person1"
SELECT `name` FROM `products` WHERE `name` = "product3";
And here is another silly thing; if I do this code :
INSERT INTO `clients` (products)
SELECT `name` FROM `products` WHERE `name` = "product3";
It creates a completely new client with every column null or empty except for the products column where it adds product3. Weird right?

Categories