how do i get data from array depending on some conditions - mysql

I have tables T1 and T2. In T1 I have column cat_id having integer values in array seperated by commma, e.g.
row1=1; row2=1,41; row3=40,1
In T2 I have column id having single integer values, e.g.
row1=1; row2=40; row3=41; row4=0
Now depending on id selected from T2 I want data from T1.
My problem is how do I apply the condition in T1. I am using CodeIgniter. I have tried like this.
$this->db->select('*');
$this->db->where('status', 'yes');
$this->db->where('cat', '1');
$this->db->like('category_id', '1');
$this->db->or_like('category_id', ',1');
$this->db->or_like('category_id', '1,');
$this->db->get('T1')->result_array();
but the problem persists as for id = 1, if there is data in T1 as 51,5 it also get selected.
How do i solve this. thanks in advance

Try using FIND_IN_SET
SELECT
*
FROM T1
LEFT JOIN T2 on FIND_IN_SET(T2.id, T1.cat_id)
WHERE T1.status = 'yes' AND T2.cat = 1
If you think the extra records are coming you can use INNER JOIN instead of LEFT JOIN.
IN COdeigniter
$sql_query = "
SELECT
*
FROM T1
LEFT JOIN T2 on FIND_IN_SET(T2.id, T1.cat_id)
WHERE T1.status = 'yes' AND T2.cat = 1
";
$this->db->query($sql_query);

As to the above question,using in_array() also helps.
$this->db->select('cat_id');
$row = $this->db->get('T1')->result_array();
$list = array();
foreach($row as $ind=>$val){
$c_id = explode(',', $row['cat_id);
if(in_array('1', $c_id){
array_push($list, $val)
}
}

Related

multiple sort fields on multiple tables without join sql

How to do multiple sort without Join on tables? I'm able to sort fine by audDt but I'm unable to do a secondary sort on siteSt. Here is my code:
$sorc451sql = "Select * FROM table1 ORDER BY audDt";
while ($sorc451row = mysqli_fetch_array($sorc451res)) {
$sorc451arycust[$i][6] = $sorc451row['gropID'];
$sorc452sql = "Select * FROM table2 WHERE gropID=".$sorc451arycust[$i][6];
while ($sorc452row = mysqli_fetch_array($sorc452res)) {
$sorc452arycust[$i][12] = $sorc452row['siteSt'];
}
}
Its a quick fix:
$sorc451sql = "Select t1.gropID AS gropID, t2.siteSt AS siteSt FROM table1 t1 JOIN table2 t2 ON t1.gropID=t2.gropId ORDER BY audDt, SiteSt";
while ($sorc451row = mysqli_fetch_array($sorc451res)) {
$sorc451arycust[$i][6] = $sorc451row['gropID'];
$sorc452arycust[$i][12] = $sorc452row['siteSt'];
}
It looks like table1 isn't used so the following looks sufficient as a query:
$sorc451sql = "Select gropID, siteSt FROM table2 ORDER BY audDt, SiteSt";

MySQL, display data not shown when using '=' command

Is there a way for me to also print out values not equal to each other?
I used a command:
WHERE displayname = personnel
I have 800+ data not shown, how can I show these data?
Note personnel and data are from two different tables.
My whole script looks like this:
SELECT a.something, a.somethins2, b.something1, b.something2
FROM a, b
WHERE a.displayname = b.personnel
!= is the "not equals" operator, so you could have a WHERE displayname != personnel clause. A full query would probably look like this:
SELECT displayname, personnel
FROM some_table t1
JOIN some_other_table t2 ON t1.id = t2.id
WHERE displayname != personnel
Try with this
select * from table_name1 t1,table_name2 t2 where
column_name!='".$data."'
and t2.id=t1.id
if you are trying to get the record that matched a.displayname = b.personnel and not matched from table a:
SELECT a.something, a.somethins2, b.something1, b.something2
FROM a LEFT JOIN b
ON a.displayname = b.personnel

How to correlate data from two separate tables

I am a beginner at SQL. I am trying to write the following statement, but where user is in group (so where group_members.user_id = $_SESSION['user_id']).
SELECT `group_name`, `group_description` FROM `groups`
How do I write this sort of query that relates values from two different tables? Sorry if I haven't explained this well.
EDIT: When I use the query provided by Matthew Haugen, I seem to have the following difficulty: only one set of results is returned in an array.
This is my code for the function that does the query:
function group_summary(){
$sql = secure_mysqli("SELECT `group_name`,`group_description` FROM `groups` WHERE `group_id` IN (SELECT `group_id` FROM `group_members` WHERE `user_id` = ?)", array($_SESSION['user_id']), "i");
$groups = $sql;
return $groups;
}
And this is secure_mysqli:
function secure_mysqli($query, $values, $datatypes) {
global $link;
$stmt = mysqli_prepare($link, $query);
if($stmt !== false){
if(is_array($values)){ foreach ($values as &$value) $ref_values[] = &$value;
call_user_func_array("mysqli_stmt_bind_param", array_merge(array($stmt, $datatypes), $ref_values));
mysqli_stmt_execute($stmt);
return #mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));
}}}
It sounds like you want either this
SELECT * FROM Groups
WHERE Groups.Id IN (SELECT GroupId FROM Group_Members WHERE User_Id = #userid)
Or this
SELECT * FROM Groups
INNER JOIN Group_Members ON Groups.Id = Group_Members.GroupId
WHERE Group_Members = #userid
You will have to perform what is called a JOIN, it depends on what type you want, see here.
So you can do something like the following
SELECT A.GROUP_NAME
, B.GROUP_DESCRIPTION
FROM TABLE1 A
JOIN TABLE2 B
ON A.UNIQUE_KEY = B.UNIQUE_KEY
WHERE GROUP_MEMBERS.USER_ID = $_SESSION['USER_ID']

SQL update table using select from and multiple joins

Here are the relevant columns of my tables
bia_panels ( id, sign_id, value, project_id )
bia_clients ( id, name )
bia_projects ( id, name, client_id, city_id )
bia_cities ( id, name )
I am attempting to update the bia_panels.project_id to the bia_projects.id where the bia_panels.value = bia_clients.name and the panels.project_id =000 and the value is not blank of course I must use multiple joins to get there
-- UPDATE
SELECT * FROM
`bia_panels` AS t1
JOIN bia_clients AS t2
ON t1.value = t2.name
JOIN bia_projects AS t3
ON t2.id = t3.client_id
-- SET t1.project_id = t3.id
-- WHERE t1.value<>'' AND t1.project_id = '000'
WHERE t1.value <>''
The problem is that this is not giving me the correct results (my project ids are not correct somewhere in the joins multiple results are returned so they break
I know that once I am able to get the select portion correct I can use an update
For example there may be multiple panels where the value=client.name but not all of them are the same project ID
and bia_panels.ID = bia_panels.project_id
join condition is missing in your select query, this should be added like
JOIN bia_projects ON bia_clients.id = bia_projects.client_id and bia_panels.ID = bia_panels.project_id
following query should give right output
SELECT sign_id, value, left(sign_id, 3) AS city_ID ,
bia_clients.id AS 'client id', bia_projects.id AS proj_id , bia_cities.id
FROM bia_panels
JOIN bia_clients ON bia_panels.value = bia_clients.name
JOIN bia_projects ON bia_clients.id = bia_projects.client_id and bia_panels.ID = bia_panels.project_id
JOIN bia_cities ON bia_projects.city_id = bia_cities.id WHERE bia_panels.value<>'' AND bia_panels.project_id = '000' ORDER BY value
I would little bit reorganize your query into UPDATE query:
UPDATE bia_panels p, bia_clients c, bia_projects t
SET p.project_id=t.id
WHERE p.value=c.name
AND t.client_id=c.id
AND p.project_id=''

combing two SELECT statements into a JOIN

I have two tables: table_1: item_id, name ... table_2: image_id, item_id, url
The following function retrieves the 'urls' of the items with the '$name' that is passed in. I first need to get the 'item_id' from table_1 to use in the query. I only need to return the 'url's from table_2.
It currently works fine, but I feel it could be streamlined to be done in one query with a JOIN rather than two separate queries. I've tried using different 'JOIN's to make it work but I can't seem to get it right.
Here's the function as it stands now with the two queries...
function get_items( $name ) {
global $wpdb;
$sql1 = "SELECT `item_id` FROM `table_1` WHERE `name` = '$name'";
$results1 = $wpdb->get_var( $sql1 );
$sql1 = "SELECT `url` FROM `table_2` WHERE `item_id` = '$results1'";
$results2 = $wpdb->get_results( $sql1, ARRAY_A );
return $results2;
}
and here is the 'JOIN' that I tried implementing, but was unsuccessful (I've also switched it around and did a 'LEFT/RIGHT JOIN' as well resulting in the same result) ...
$sql1 = "SELECT `table_2`.`url`
FROM `table_2`
INNER JOIN `table_1`
ON `table_2`.`item_id` = `table_1`.`item_id`
WHERE `table_1`.`item_id` = '$name'";
Any advice on combining these two queries?
The problem with your query is this WHERE table_1.item_id = '$name', it should be the name not the item_id
SELECT b.url
FROM table1 a
INNER JOIN table2 b
ON a.item_id = b.item_id
WHERE a.name = '$name'
select url from table_2 where item_id IN (select item_id from table_1 where name = $name )