Zend Framework: select distinct rows by two fields - mysql

I have in a dbtable
field 1 field 2
1 2
2 3
2 3
2 2
1 1
2 2
I want to get
field 1 field 2
1 2
2 3
2 2
1 1
Tried
$select = $this->_dbTable->select()->from($this->_dbTable,array("DISTINCT(field_1) as field_1","DISTINCT(field_2) as field_2"));
$select = $this->_dbTable->select()->from($this->_dbTable,array("DISTINCT(field_1, field_2) as field_1, field_2"));
PS: Why this Zend Framework's so hard?!

As Sashi Kant suggested this can be done by a group by on field_1, field_2. Here is how to make it using Zend DB :
$select = $this->_dbTable->select()->from($this->_dbTable, array("field_1", "field_2"))
->group(array("field_1", "field_2"));

Try this :
Select
field1,
field2
from mytable
group by field1, field2

You can use Zend_Db_Expr too
Try this:
$select = $this->_dbTable->select()->from($this->_dbTable, new Zend_Db_Expr('DISTINCT(field_1) as field_1'));

Related

how to select data from two tables in mysql

I have two tables in my SQL Server database. The first is questions and second is question_options.each question 4 options There is a column qid in both tables.
my db structure:
questuons table:
qid q_text discription
1 what is ip some data
2 what is ipv same data
and question_options table like this:
oid qid options correct_answer
1 1 option1 0
2 1 option2 0
3 1 option3 1
4 1 option4 0
5 2 example1 0
6 2 example4 1
7 2 example3 0
8 2 example2 0
how can fetch questions and question_options table data and how can display
fetched data somthing like this:
<div id="qid">qid</div>
<div id ="q_text">q_text</div>
<div id="options1">option1</div>
<div id="options2">option2</div>
<div id="options3>option3</div>
<div id="options4>option4</div>
<div id="correct_answer">correct_answer</div>
<div id="discription">discription</div>
how to select qid,q_text from table1 and 4 options where qid from table2 with one sql statement?
You can fetch the data using join over QID and use the SUBSTRING to subtract the character string as below:
If you want to fetch all the correct answer you can use:
select
q.qid,
q.q_text,
qo.options,
if(qo.correct_answer = 1,substring(qo.options,length(qo.options)),null) as correct_answer,
q.description
from questions q,question_options qo
where q.qid = qo.qid
If you want to fetch data for correct_answer only then you can use:
select
q.qid,
q.q_text,
qo.options,
if(qo.correct_answer = 1,substring(qo.options,length(qo.options)),null) as correct_answer,
q.description
from questions q,question_options qo
where q.qid = qo.qid
and if(qo.correct_answer = 1,1,null) is not null
If you want to get the data for single question you can and one more condition at last:
and q.qid = 1
Demo
select
q.qid,
q.q_text,
qo.options,
IF(qo.correct_answer = 1, substring(qo.options, 7, 1), 0)
q.discription
from questions q
join question_options qo on q.qid=qo.qid
Query should be like below
$query = SELECT * FROM questuons LEFT JOIN question_options ON questuons.qid=question_options.qid where question_options.correct_answer= '1'";
and php code should be like below
$result = $DBconn->query($query );
while($row=$result->fetch_array()) {
echo $row['q_text'];
}
Try with the above. Hope it will work.

Not an Intersect (IN) but sort of. Where Col a row x intersects with col b.Mysql

I have a matrix table. 6 columns wide and 6 rows deep.The first column has the numbers 0, 5,4,3,2,1 in that order. The first row in the table is 0,1,2,3,4,5
so like this:
Column names RM0 RM1 RM2 RM3 RM4 RM5
0 1 2 3 4 5
5
4
3
2
1
each of the rows has words in it ie, MEDIUM, HIGH, CRITICAL etc.
I have a form the user fills out that requires them to give a rating from 1-5 in two categories. Their selections correspond to one of the words in the matrix. SO..after all that...I need to be able to select from the table something like this:
SELECT FROM TBLX WHERE RM0=4 INTERSECTS WITH RM4
I have look at IN but everything I see so far still requires you to have another WHERE x=y part. Could I dump the whole table into an array somehow get what Im after? As you can see, Im clueless in this regard.
You can do something like this:
SELECT CASE :b2
WHEN 1 THEN t.rm1
WHEN 2 THEN t.rm2
WHEN 3 THEN t.rm3
WHEN 4 THEN t.rm4
WHEN 5 THEN t.rm5
ELSE ''
END AS `the_word`
FROM TBLX t
WHERE t.rm0 = :b1
I'm confused by the table design. And even more confused by the first row... if you want to get that incorporated in the query, that would be inordinately complex.
In terms of a normal relational design, we'd represent this as rows for each pair of values, with a single word on each row.
mymatrix
x_rating y_rating the_word
-------- -------- --------
1 1 LOW
1 2 LOW
1 3 MEDIUM
1 4 MEDIUM
1 5 MEDIUM
2 1 LOW
2 2 MEDIUM
2 3 MEDIUM
...
5 5 HIGH
An example of a query to get "the word" would be something like this:
SELECT mx.the_word
FROM mymatrix mx
WHERE mx.x_rating = :b1
AND mx.y_rating = :b2
To use that same query pattern with the original table, we could get that original table converted to look like the mymatrix table, using the values from the RM1 through RM5 columns in the RM0=0 row as the y_rating value. The SQL to do that is rather cumbersome:
SELECT mx.the_word
FROM (
SELECT t1.rm0 AS x_rating
, s1.rm1 AS y_rating
, t1.rm1 AS the_word
FROM mytable t1
JOIN mytable s1
ON s1.rm0 = 0
AND t1.rm0 > 0
UNION ALL
SELECT t2.rm0 AS x_rating
, s2.rm1 AS y_rating
, t2.rm1 AS the_word
FROM mytable t2
JOIN mytable s2
ON s2.rm0 = 0
AND t2.rm0 > 0
UNION ALL
SELECT t3.rm0 AS x_rating
, s3.rm1 AS y_rating
, t3.rm1 AS the_word
FROM mytable t3
JOIN mytable s3
ON s3.rm0 = 0
AND t3.rm0 > 0
UNION ALL
SELECT t4.rm0 AS x_rating
, s4.rm1 AS y_rating
, t4.rm1 AS the_word
FROM mytable t4
JOIN mytable s4
ON s4.rm0 = 0
AND t4.rm0 > 0
UNION ALL
SELECT t5.rm0 AS x_rating
, s5.rm1 AS y_rating
, t5.rm1 AS the_word
FROM mytable t5
JOIN mytable s5
ON s5.rm0 = 0
AND t5.rm0 > 0
) mx
WHERE mx.x_rating = :b1
AND mx.y_rating = :b2
I ended up taking a different route. I still used the matrix table but instead of trying to access when I need to retrieve information I queried the table once, took all the information and put it into an array like this:
sql = "SELECT * FROM `tblriskmatrix`";
if (!$conn->query($sql)) {
echo "query failed: (" . $conn->errno . ") " . $conn->error;
}
$result = $conn->query($sql);
$arrayOfMatrix = array();
while ($row = $result->fetch_assoc()) {
$arrayOfMatrix[] = $row;
}
I then put the array into a session variable like this:
$_SESSION['arrayOfMatrix'] = $arrayOfMatrix;
I then created some functions to get the keys pressed and passed them to another function that did an ajax call, passing the variables of the key presses script that took those variables and passed them into my session variable and returned the desired result. Like this:
session_start();
$a = $_GET['var_a'];
$b = $_GET['var_b'];
$response = array('response' => $_SESSION['arrayOfMatrix'][$a]["RM$b"]);
echo json_encode($response);
The response is then passed to a form element and displayed. So with the matrix table in the session variable as an array I can easily extract the contents of what is in cell of row x within column y. Not sure if its the most technically correct route but it works.

Searching for multiple numbers in multiple number field

-----ID
1
5
1,6
3
4
1,36
1
I have these '1,6,36' number to search.
It should find any row that contains 1 OR 6 OR 36 (e.g. total 4 rows in the above table)
I tried
FIND_IN_SET(ID, '1,6,36')
ID IN (1,6,36)
None of them worked.
Any idea how to achieve this ?
FIND_IN_SET(1, ID) OR
FIND_IN_SET(6, ID) OR
FIND_IN_SET(36, ID)
You have to do a sql query called IN, and you should do it as follows:
$id_array = array(1, 6, 36)
$sql = "SELECT * FROM table WHERE id IN ($id_array)";
$search = mysqli_query($sql_connection, $sql);
Then to display, you have to do a PHP loop like follows:
while($ids = mysqli_fetch_array($search)) {
echo '<h3>'.$ids['field_from_db'].'</h3><br />';
}

CodeIgniter Fetching Records From More Than Two Tables

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

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

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