first of all i have 1 table in database.
1)tags :
id name
1 theme1=test1
2 theme1=test2
3 theme1=test3
4 theme2=test1
5 theme2=test2
6 theme2=test3
And i have bunch of id of tags in array. like 1,3.
Now,
1)select name from tags where id=1
result: theme1=test1
(now using wildcard)
2)select id from tags where name like 'theme_test1'
result : 1,4
(here 'theme_test1' need to take from query1)
I am getting output proper but need to use 2 query.I want to do this in single query.
Thanks
SELECT id FROM tags WHERE name LIKE (
SELECT CONCAT(SUBSTRING(name,1,5),'__',SUBSTRING(name,8)) FROM tags WHERE id=1
)
Returns 1,4
But Two queries (or a refactor) might be a better option
If you whant to get the id with the same value you can try this:
SELECT t2.* FROM yourtable t1
JOIN youtable t2 on ON t2.name like concat(substr(t1.name,1,5), '%', substr(t1.name,8))
WHERE t1.id=1;
For performance better use this:
SELECT t.id
FROM r
INNER JOIN r AS t ON t.name LIKE CONCAT('theme_=test', SUBSTRING(r.name,-1))
WHERE r.id = '1'
r is your table in this case.
NOTE: this answer isn't valid in case that you have theme1=test1 and theme1=test10 values.
maybe you can use query :
select id from tags where name = (select name from tags where id = 1 ).
You can try that query.
Related
I have a similar question to How to use GROUP BY to concatenate strings in MySQL? , however for this example for mytable table
id string aggr
1 A NULL
1 B NULL
2 F NULL
The difference is I need to update the table to get this results:
id string aggr
1 A A|B|
1 B A|B|
5 C C|E|C|
5 E C|E|C|
5 C C|E|C|
2 F F|
As result the same id we have the same newcolumn values.
It is absolutely fine to add a delimiter |at the very end of the string. That way I can even faster count how many "items" are in the newcolumn without adding +1 because of the absense at the very end (just in between). Also I won't care about validation (if I have a pipe right before) when appending another part or two into aggr column.
Thank you.
You can try this query :
UPDATE my_table t
SET aggr = (
SELECT * FROM (
SELECT CONCAT(GROUP_CONCAT(t2.string SEPARATOR '|'), '|')
FROM my_table t2
WHERE t2.id = t.id
) AS X
)
You could a group_concat joined on original table
select a.id, a.string , b.aggr
from my_table a
inner join (
select id, group_concat(string SEPARATOR '|') as aggr
from my_table
group by id
) b on a.id = b.id
I have the following query:
SELECT question_code FROM table_questions WHERE question_code IN (1,2,3,111,222);
Here, values (1,2,3,111,222) are coming from PHP array.
The output for the above query is:
question_code
1
2
3
I want the output to be the question_codes which are not in the table and present in the Array.
i.e. I want the output to be question_code which do not exist in the table.
question_code
111
222
I know this problem can be handled in PHP after retrieving the data from the Table. But as I may have large number of tuples, solution which can take care of this thing at query level would be helpful.
You will get the record of SELECT question_code FROM table_questions WHERE question_code IN (1,2,3,111,222); in array and values (1,2,3,111,222) are coming from PHP array.
Use array_diff to compare two arrays and gets the difference.
For reference http://www.w3schools.com/php/func_array_diff.asp
If you want to use plain SQL you have to use a LEFT JOIN:
SELECT c.question_code
FROM (SELECT 1 AS question_code
UNION ALL select 2
UNION ALL SELECT 3
UNION ALL SELECT 111
UNION ALL SELECT 222) AS c
LEFT JOIN table_questions q
ON c.question_code = q.question_code
WHERE
q.question_code IS NULL
you can create the subquery SELECT .. UNION ALL .. dynamically, like this:
<?php
$ids = array(1,2,3);
$subquery = str_repeat('SELECT ? AS q UNION ALL ', count($ids) - 1) . 'SELECT ? AS q';
$sql = <<<SQL
SELECT c.q FROM ($subquery) AS c
LEFT JOIN table_questions q
ON c.q = q.question_code
WHERE
q.question_code IS NULL
SQL;
$stm = $db->prepare($sql);
$stm->execute($ids);
$data = $stm->fetchAll();
?>
but it might not be too pratical... the only alternative is to process the returned question_codes codes in php.
How can I select one row from that table http://i.stack.imgur.com/27cu9.jpg where values of 'user_1' and 'user_2' may look like
user_1 user_2
1 2
2 1
In other words I want to select a field that contains 2 users with submitted=1 no matter in which field the value is.
Here is a simple query that does this:
select *
from t
where submitted = 1 and 2 in (user_1, user_2)
If I understood your question, I think you need to JOIN the table on itself if you are trying to return rows that have corresponding users (1,2) and (2,1):
select t1.*
from yourtable t1
join yourtable t2 on
t1.user_1 = t2.user_2 and t1.user_2 = t2.user_1
SQL Fiddle Demo
If however you are just trying to see if user 2 exists in either of the fields, then look at Gordon's post.
Use this:-
select * from tblname as t1, tblname as t2 where
t1.user_1 = t2.user_2 and t1.user_2 = t2.user_1 and t1.user_1<>t1.user_2
EDIT:-
Updated the query so that the rows with the same values do not appear in the result.
I'm very new with SQL and need assistance on how I can accomplish this task using the correct query.
I have 2 tables that I need to use. Table "TB1" has:
id Name
1 bob
2 blow
3 joe
table "TB2" has:
compid property
1 bob
2 blow
I am trying to get which compid is missing in "TB2" and insert it from "TB1"
the query I am doing is:
SELECT id, name from TB1, TB2 where id <> compid
what I get is 2 ouputs of Id 1, and 2, and 3 outputs from id 3. by using php:
for($i=0;$i <= mysql_num_rows($comp)-1; $i++)
{
echo mysql_result($comp, $i, 0)."<br>";
}
and I expected the ouput 3 but instead got this:
1
1
2
2
3
3
3
I understand its comparing all the rows within the table but is there a way to achieve what I am looking for?
Thanks for your time.
You are performing an implicit Cartesian JOIN which results in every row against every other row. You need to specify what attribute JOINs the two tables.
Using implicit syntax (not recommended):
SELECT id, name
FROM TB1, TB2
WHERE id <> compid
AND TB1.Name = TB2.property <-- Column join
Using explicit syntax:
SELECT id, name
FROM TB1
JOIN TB2
ON TB2.property = TB1.Name <-- Column join
WHERE id <> compid
To accomplish your goal you would need something along the lines of:
SELECT TB1.id, TB1.name
FROM TB1
LEFT JOIN TB2
ON TB2.property = TB1.Name
WHERE TB2.compid IS NULL
See it in action
It's best practice to always alias the columns you select to prevent ambiguity.
To select it you can do:
SELECT *
FROM TB1
WHERE id NOT IN (
SELECT compid
FROM TB2
);
I am hoping for some help making my queries more efficient if possible. I have two tables. One which contains the types of content available and another which contains content linked to the type table by a type id.
I am trying to select from the content table and group the results by type_id. I am currently using a query inside a loop to do this so it is selecting again from the second table for each type_id. Is there a better/more efficient way to do this?
Here are my current tables and queries:
type_id type
1 type 1
2 type 2
id title type_id content
1 test1 1 test content 1
2 test2 2 test content 2
3 test3 1 test content 3
$query="select type_id, type from type_table";
foreach($query as $type){
$query="select title, content from content_table WHERE type_id=$type['type_id']";
}
I hope this makes sense.
Thanks
Your example appears to select all possible values from the content table, albeit in batches of a given type at a time. So select title, content from content_table would surely do this whole thing in one go, without looping.
To put each type_id into a new div, you could do something like this:
SELECT title, content, t.type_id, t.type
FROM content_table c
JOIN type_table t ON t.type_id = c.type_id
ORDER BY t.type_id
// pseudo-code
prev_type_id = -1
print "<div>"
foreach row:
if type_id != prev_type_id:
prev_type_id = type_id
print "</div><div>"
print "</div>"
(edited based on comments)
Use a join and order by. Something like (no guarantee for syntactical correctness):
SELECT
c.title, c.content, t.type_id, t.type
FROM
type_table t, content_table c
WHERE
c.type_id = t.type_id
ORDER BY
t.type_id
Sure, you could join the two tables.
In this example, however, it doesn't appear that you even need to use the type_table. You could just sort your query by type_id:
select title, content from content_table order by type_id;
What is the expected result ?
This is what I think you want.
$sql = 'SELECT t.type, c.title, c.content
FROM `content_table` c
INNER JOIN `type_table` t ON c.type_id = t.type_id
ORDER BY c.type_id ASC'
// Dont use a foreach-loop to loop over the results. While-loop is ideal
while ($row = <query or fetch from resultset here>) {
..
}