I have a table has columns tag_id and item_id,
---------------------
| tag_id | item_id |
---------------------
| 1 | 2 |
| 2 | 2 |
| 4 | 2 |
| 3 | 5 |
| 5 | 7 |
| 11 | 5 |
---------------------
For example, I want to return item_id = 2 if I input tag_id = 1,2,4
what is the query should look like?
I am using Codeigniter.
If you are looking for Msql query that returns only one cell value you can use the following:
SELECT DISTINCT item_id FROM tableName WHERE tag_id IN (1, 2, 4)
Try this code.Will help you
$this->db->select('item_id');
$this->db->from('tblNAME');
$where = "tag_id='1' OR tag_id='2' OR tag_id='2 ";
$this->db->where($where);
return $this->db->get()->result();
Try this:
select * from table where tag_id in (1,2,4)
Thank you all answers but unfortunately not suitable for the use. Because I want it return exactly the item id = 2 if tag id = 1,2,4.
If we using IN(1,2,4) it will return item ids that has tag id 1 or 2 or 4.
Finally I figure out the SQL query should be
Select *, COUNT(item_id) AS id_count
FROM TABLE_NAME
WHERE tag_id IN(1,2,4)
GROUP BY item_id HAVING id_count = 3
Therefore, express it in a general form, we simply replace the 3 by the size of the array of tag id.
Related
I have a table with many columns. Each row contains a unique ID field. How can I select 5 rows without selecting a row with id stored in variable $doNot.
Currently I am using the following query.
$qry="SELECT title, link FROM posts WHERE id > (SELECT MAX(id) - 5 FROM news)";
Consider this table:
id | title | link
_____________________________________
1 | title_1 | link_1
2 | title_2 | link_2
3 | title_3 | link_3
4 | title_4 | link_4
5 | title_5 | link_5
6 | title_6 | link_6
7 | title_7 | link_7
8 | title_8 | link_8
I want to select 5 rows except row with id '3' using the above query from the above table
SELECT title, link FROM posts WHERE NOT (id = $doNot) LIMIT 5
Use LIMIT to specify the number of rows to be returned:
SELECT title, link
FROM posts
ORDER BY ID DESC
LIMIT 5
If $doNot is a table variable, you should be able to simply sub-query on posts.id:
SELECT title, link FROM posts WHERE id NOT IN (SELECT id FROM $doNot) LIMIT 5;
I have a huge number of rows that I'd like to get say, last 5 records inserted in that database from 10 different users. If the same user inserted the last 3 rows into database, we must get one row, skip the others two and move to get a row per user, until it count up to 5.
A database like that:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
1 | 4 | baa
4 | 5 | baa0
5 | 6 | baa1
5 | 7 | baa2
6 | 8 | baa3
7 | 9 | baa4
Should return:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
4 | 5 | baa0
5 | 6 | baa1
The current filter was done by PHP, like this:
$used = array();
while ($data = mysql_fetch_array($query)) {
$uid = $data['user_id'];
if(in_array($uid, $used))
continue;
array_push($used, $uid);
// do something with data
}
But I want to refactor it, and do the filter purely by mysql, if possible. I don't know much MySql and that's why I'm having problem to archive this...
Here's what I've tried
select DISTINCT(user_id), news_id, title from XXX
WHERE GROUP BY (news_id) DESC
LIMIT 0,5
How can I do that?
1 way you can do it is to generate a partitioned row number per user and then select 5 records where RowNumber = 1.
SELECT *
FROM
(
SELECT
d.user_id
,d.news_id
,d.title
,(#rn:= if(#uid = user_id, #rn + 1,
if(#uid:=user_id,1,1)
)
) as RowNumber
FROM
Data d
CROSS JOIN (SELECT #uid:=-1, #rn:=0) vars
ORDER BY
user_id
,news_id
) t
WHERE
t.RowNumber = 1
ORDER BY news_id
LIMIT 5;
http://rextester.com/JRIZI7402 - example to show it working
Note you can change the row order by simply changing the ORDER BY statement of the derived table so if you have a column that will signify the latest record e.g. an identity column or a datetime column you can use that, but user_id must be the first criteria to be partitioned correctly.
Do it from your query.
"SELECT * FROM table GROUP BY user_id ORDER BY news_id DESC LIMIT 5"
well, i think this will achieve what you are after.
select user_id, news_id, title from tableName
GROUP BY user_id
ORDER BY news_id DESC
LIMIT 0,5
Hope this helps!
How to query for erase the view below?
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | 2 |
+-------------------+------------+
To be like this:
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | |
+-------------------+------------+
When displaying results but not entered into the database.
A simple way is:
select DISTINCT order_id, weight from xyz
UNION
select order_id, null from xyz
group by order_id, weight
having count(*) > 1
Order by weight desc;
The 1st select statement will display all the unique values and 2nd one will retrieve only the repeated values.
In your required output table, it seems like you want to display all the non-repeated rows and the 1st column value of repeated rows but not 2nd column value. The above query will allow you to do that.
OK, here is how to do it:
SELECT
Order_id,
Weight,
if(#order_id = Order_id, '', Weight) as no_dup_weight,
#order_id := Order_id as dummy
FROM Table1
ORDER BY Order_id asc;
You basically need to check to see if the previous Order_id is the same as the current, and if they are, output an empty field.
Here is an SQLFiddle demonstrating the solution.
Do you actually need 2 rows for the dupes? Can't you just use the DISTINCT clause as per http://www.mysqltutorial.org/mysql-distinct.aspx
Or is it important to know what has duplicates. In which case you should look into the GROUP BY clause
i already done everything to remove this duplicity on the database
On selecting a checkbox on the sectio "Bairros" i utilized as Array
for($m=0; $m<count($_POST["bairros"]); $m++){// LOOP 1
$pesquisar=($_POST["bairros"][$m]);
//Copy bairros(Array) and esporte (POST)
$query = "SELECT DISTINCT * FROM cadastro WHERE
(esporte1 = '".$_POST["esportes"]."' OR
esporte2 = '".$_POST["esportes"]."' OR
esporte3 = '".$_POST["esportes"]."' OR
esporte4 = '".$_POST["esportes"]."')
AND
(bairro1 = '".$pesquisar."' OR
bairro2 = '".$pesquisar."' OR
bairro3 = '".$pesquisar."' OR
bairro4 = '".$pesquisar."')
AND
ativarAparecer='sim' ORDER BY nomeCompleto ASC LIMIT 20";
$esporte= new consultar();
$esporte->executa($query);
//Loops
for($l=0; $l<$esporte->nrw; $l++){ //LOOP 2
echo $esporte->data["nomeCompleto"]."<br />";
$esporte->proximo();
} //close LOOP2
} //close LOOP1
Detail: this function object oriented, I believe that i'm doing something wrong at SQL or MYSQL, perhaps something is missing there.
SELECT DISTINCT *
Stop There. DISTINCT * can do what? Duplicate of what? it cant do that. Give it a field name to see unique values.
For example
SELECT DISTINCT nomeCompleto
Let's break this down. The DISTINCT clause will return unique sets based on the selected columns.
Let's say you have a table:
a | b | c
=========
1 | 2 | 3
1 | 1 | 3
1 | 2 | 4
Now if you SELECT DISTINCT a FROM table, you would get:
1
but if you SELECT DISTINCT a, b FROM table, you would get:
a | b
=====
1 | 2
1 | 1
That's because {1,2} is different from {1,1}, even though the a column is the same between those two sets.
Obviously, doing SELECT DISTINCT * FROM table would give you the original table because it uses all three columns as a "composition" of the unique set. If we amended the table to look like this:
a | b | c
=========
1 | 2 | 3
1 | 1 | 3
1 | 2 | 4
1 | 2 | 3
Then your result of SELECT DISTINCT * FROM table would give:
a | b | c
=========
1 | 2 | 3
1 | 1 | 3
1 | 2 | 4
because of the duplicate result set of {1, 2, 3}. However, since most tables have an auto-incrementing identifier as the primary key, there is almost always no difference between SELECT * and SELECT DISTINCT *.
Perhaps you're looking to GROUP BY a certain column?
How would I be using GROUP this in my script? Column that there are several equal records are this bairro, bairro2, bairro3, bairro4. Inside it is in numbers
bairro1 | bairro2 | bairro3 | bairro4
14 | 14 | 15 | 27
34 | 15 | 14 | 30
27 | 45 | 12 | 14
Hello there, I have a schema like this, table name feeds
Where msg_id is unique and its the primary key of the table
| msg_id |commented|
| 1 | 10 |
| 2 | 10 |
| 3 | 10 |
| 4 | 21 |
I want to build a query that would select the last two rows
The output should go like this
| msg_id |commented|
| 3 | 10 |
| 4 | 21 |
In short the query should return the rows with msg_id which have a distinct commented value
Group by the column ment to be unique and select the highest id for every group
select max(msg_id) as msg_id, commented
from your_table
group by commented
Try this -
select max(msg_id), commented from your_table group by commented
SELECT * FROM feeds GROUP BY commented