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.
Related
I have many tables with the same structure for each of my costumers.
All information are distinct for each table.
For some reason, I need to create some temporary table with the same structure with 200 values everytime I run.
So let's assume I have 3 tables.
Costumer1, Costumer2, Costumer3.
All those tables have id, user_name, contact_name, contact_email, sent1, sent2, sent3, sent4, status.
I need some query to put inside costumer_tmp, only 200 values in total, from all those 3 tables, everytime I run the script. And everytime I run cant repeat the last values I got before.
So for example:
Costumer1
id = 29
user_name = test1
contact_name = contact1
contact_email = contact1#mail.com
sent1 = yes
sent2 = no
sent3 = no
sent4 = no
status = In Progress
Costumer2
id = 37
user_name = test2
contact_name = contact123
contact_email = contact123#mail.com
sent1 = yes
sent2 = no
sent3 = no
sent4 = no
status = In Progress
Costumer3
id = 87
user_name = test3
contact_name = contact231
contact_email = contact231#mail.com
sent1 = yes
sent2 = no
sent3 = no
sent4 = no
status = In Progress
How to Insert on costumer_tmp only 2 records of those 3 and next time I run the script don't repeat those 2 records, just insert only 1 record remaining.
Your requirement is a bit weird, but if I understand weel, how about something like : (not tested)
insert into costumer_tmp
select * from Costumer1 one where one.id not in (select id from costumer_tmp)
union all
select * from Costumer2 two where two.id not in (select id from costumer_tmp)
union all
select * from Costumer3 three where three.id not in (select id from costumer_tmp) LIMIT 200
aaiaflat_final
ID Data
1 [2,96,801]
Engine_willie2
ID Data Eng
1 2 3.0
1 96 4.0
1 801 4.6
1 999 5.0
Select
Engine_willie2.ID,
(
CASE
WHEN Engine_willie2.Data = aaiaflat_final.Data
THEN Engine_willie.Data END
)
AS Data,
(
CASE
WHEN Engine_willie2.Data = aaiaflat_final.Data
THEN Engine_willie.Eng END
)
AS Eng
FROM aaiaflat_final
Left Join Engine_willie2 ON
Engine_willie2.ID = aaiaflat_final.ID
WHERE
aaiaflat_final.ID <> ''
I would like the query to look like this
ACES_Query
ID Data Eng
1 2 3.0
1 96 4.0
1 801 4.6
I'm not sure how to parse the [2,96,801] in the aaiaflat_final table as the query works fin if the Data is 2 or any other exact matching data to the data column in Engine_willie2.
Any help would be greatly appreciated.
Joining the tables on id, using replace() to get rid of the brackets and then find_in_set() produces the desired result from the sample data.
SELECT e.id,
e.data,
e.eng
FROM engine_willie2 e
INNER JOIN aaiaflat_final a
ON a.id = e.id
WHERE find_in_set(e.data, replace(replace(a.data, '[', ''), ']', ''));
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.
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'));
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();