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();
Related
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.
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);
-----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 />';
}
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 have a table in MySQL named sale with 3 columns like below :
sale:
id|customers|type
I want to select sales that has type!=2 OR
sales that has type=2 and customers!=0
I write statement like this :
sale.type != 2 OR (sale.type = 2 AND sale.customers != 0 )
but it doesn`t give me correct rows .
Also I have to use AND for other columns in this query but the operators between all of them is AND , just here I use OR .
Just remember thus rule: bracket the OR!
where foo = 'bar'
and (sale.type != 2 OR (sale.type = 2 AND sale.customers != 0 ) )
and blah = 3
Actually your condition can be simplified to:
( sale.type != 2 OR sale.customers != 0 )
Because logically checking for sale.type = 2 is redundant.
do you want sales.type not equal to 2 or (sales.type equal to 2 when you have customers not equal to 0?)
you can use HAVING and WHERE together.
Select * from sales where type !=2 or (type=2 HAVING customers !=0);
Just I try with this i found working: sqlfiddle
select * from sale where
sale.type != 2 OR (sale.type = 2 AND sale.customers != 0 )