Mysql search using where clause in same column - mysql

Hello I'm having a problem.
I want to search values in the name column using the where clause. Example table is this with values stored.
table1
--------------------------
id | name | anotherid
--------------------------
1 | name1 | 1
2 | name2 | 1
2 | name2 | 2
I have textboxes that I can change how many they appear. For example I have two textboxes. The value on textbox one is 'name1' and the second is 'name2'
My query must find those two values in table1 and, when they're found, their anotherid column must be the same also.
From the first table, All I want to get is this:
table1
--------------------------
id | name | anotherid
--------------------------
1 | name1 | 1
2 | name2 | 1
However when a situation like this appears and I still have two textboxes with the same values:
table1
--------------------------
id | name | anotherid
--------------------------
1 | name1 | 1
2 | name2 | 1
2 | name2 | 2
3 | name3 | 1
The result must be empty.
Now back to the first table. I used this code.
SELECT * FROM `table1` WHERE name = "name1" and name = "name2"
and
SELECT * FROM `table1` WHERE name = "name1" and name = "name2" group by anotherid
but all I get are empty results.
How do you do this?

The problem is in you WHERE clause,
WHERE name = "name1" and name = "name2" will always return false. Use OR instead.
Try any of these:
1. Selecting with name1,name2 and anotherid=1
SELECT *
FROM table1
WHERE (name="name1" OR name="name2")
AND anotherid=1
The result will be:
id name anotherid
1 name1 1
2 name2 1
2. Grouping by anotherid:
SELECT * FROM table1
GROUP BY anotherid
Result:
ID NAME ANOTHERID
1 name1 1
2 name2 2
3. Grouping by name:
SELECT * FROM table1
GROUP BY name
Result:
ID NAME ANOTHERID
1 name1 1
2 name2 1

SELECT * FROM `table1` WHERE name = "name1" OR name = "name2" group by anotherid
That should do.

Try this
SELECT * FROM `table1` group by name

Related

Select rows from one table, using values from another table when present

I've got a "parameters" table like this:
id | job_id | value
1 | 100 | value1
2 | 100 | value2
3 | 100 | value3
4 | 200 | value4
5 | 200 |
6 | 200 | value7
Then, for parameters that have list values, I've got a second, list_param_items table:
id | param_id | value
1 | 5 | value5
2 | 5 | value6
I'm trying to figure out a query that would allow me to get id, job_id, and value columns, containing data from the parameters table, except when a row in the parameters table has rows in the list_param_items table with its id, concatenate those to form its value column.
So if I were to select all parameters, I'd want to get this:
id | job_id | value
1 | 100 | value1
2 | 100 | value2
3 | 100 | value3
4 | 200 | value4
5 | 200 | value5,value6
6 | 200 | value7
I also need to be able to select all params for a given job ID and get the same data, just for that job. So, for job_id = 200:
id | job_id | value
4 | 200 | value4
5 | 200 | value5,value6
6 | 200 | value7
I've accomplished it with this query:
SELECT * FROM (
SELECT parameters.id, parameters.job_id, GROUP_CONCAT(list_param_items.value) AS value
FROM jobdetail_input
INNER JOIN list_param_items
ON list_param_items.param_id = parameters.id
WHERE parameters.job_id = 200
UNION
SELECT parameters.* FROM parameters
WHERE parameters.job_id = 200
) unmerged
GROUP BY id;
The problem with this is that, in reality, there are many more columns in the parameter table than I've shown here. So I'd prefer a way to do it without having to list out all the column names. The reason I had to list them all out was to eliminate the "value" column from parameters in the first SELECT statement, so that both sides of the UNION would have the same set of columns to work with. I'd like to not list them out so the query wouldn't have to be updated anytime a column is added.
The other question I have about how efficient that query is. Even if I have to list all the parameters table columns out, is there a better way to do it?
Join the first table to a subquery of the second table which aggregates the values into a CSV string:
SELECT
p.id,
p.job_id,
COALESCE(p.value, lpi.value) AS value
FROM parameters p
LEFT JOIN
(
SELECT param_id, GROUP_CONCAT(value ORDER BY value) value
FROM list_param_items
GROUP BY param_id
) lpi
ON p.id = lpi.param_id
ORDER BY
p.id;
Demo

Copy Value from table to table MYSQL

i have two tables first table like this
Table 1 name id groups contain 2 field
id_gr
name
Table 2 name Product Contain a lot of field and contain one field name id_gr contain the id_gr Id's from table one i need to replace the Id with the id value
id_dr | name
-----------------
1 | group1
2 | group2
2 | group2
The product table is like this
product_id | name | group
-----------------------------
1 | proudct1 | 1
2 | proudct1 | 2
3 | proudct1 | 3
I need to replace the id in group in product table with its value from group table instead of id.
Try this:
UPDATE table2, table1 SET table2.group = table1.name where table2.group = table1.id
Just for clarification:
table2 is the one with 3 columns
table1 is the one with to columns

mysql - get number of occurrences from "different" query results

I was wondering how could I get the number of occurrences of a common string from different results (or using OR in my query, as example below).
table example:
id | name | rank
1 | name1 | 1
2 | name1 | 1
3 | name2 | 1
4 | name3 | 1
5 | name1 | 2
6 | name1 | 2
7 | name3 | 2
Now, I need to count number of occurrences for rank = 1 and rank = 2, without duplicating the count.
doing something like this:
SELECT name, COUNT(DISTINCT name)
AS name_num FROM table WHERE rank = 1 GROUP BY name;
results is
name1 | 1
name2 | 1
name3 | 1
perfect, but now I need to include some other result (i.e. rank = 1 OR rank = 2) and get the occurrences from each name, without duplicating it.
the wanted result for query example using table example and rank = 1 OR rank = 2 should be:
name1 | 2
name2 | 1
name3 | 2
I'll try to explain the result I want:
name1 is present when rank = 1 (+1) and when rank=2 (+1);
name 2 is only present when rank=1
name3 is present when rank = 1 (+1) and when rank=2 (+1);
Is it possible?
Select Name,
Count(Distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
Sql Fiddle Demo
You want COUNT(DISTINCT rank), not COUNT(DISTINCT name). Since you're grouping by name, there will only be one distinct name in each group.
SELECT name, COUNT(DISTINCT rank) name_num
FROM table
WHERE rank in (1, 2)
GROUP BY name
Select Name,
Count(distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
This is what you asked for?

MySQL Select with Subquery

I have 2 table in the database: mainTable and classificationTable:
mainTable:
id | classification_id | name
---------------------------------------
1 | 1 | Name1
2 | 2,3,4 | Name2
3 | 1,4 | Name3
4 | 4 | Name4
classificationTable:
classification_id | class_name
---------------------------------------
1 | Class Name1
2 | Class Name2
3 | Class Name3
4 | Class Name4
I want to get a select for example for row with ID 3 from mainTable like:
id = 3
class_name = Class Name1, Class Name4
Name = Name3
I try this select, but this return only first elemnt from array (fore exempla for row with ID 3, this return only Class Name1)
SELECT i.*,
(SELECT GROUP_CONCAT(cl.class_name) FROM classificationTable as cl WHERE cl.classification_id IN(i.classification_id)) as class_name
FROM mainTable as i;
Help PLZ.
Yes, you have a poorly designed database. But you can do what you want.
This requires two steps. The first is to join the mainTable to the classificationTable by looking for each classification_id in the list in the mainTable. You can do this using the like operator.
The second step is to bring all the class names back into one column. This is handled using group_concat.
The following query accomplishes this (although it is not tested, so it might have a typo):
select mt.id, mt.name, group_concat(ct.class_name separator ', ')
from mainTable mt join
classificationTable ct
on concat(',', classification_id, ',') like concat('%,', ct.classification_id, ',%')
group by mt.id, mt.name
The problem is your mainTable, which not only violates the 1st normal form but goes even beyond because it concatenates values in the same field (textbook example of a 1NF violation often includes multiple fields, which is bad enough).
The classification_id column should not be a concatenation of values but you should have a new table where you have a separate row for each classification_id and it should be tied into mainTable by id.
E.g.
id | classification_id
---------------------------------------
1 | 1
2 | 2
2 | 3
2 | 4
3 | 1
3 | 4
4 | 4
Once you've done that, your query will be much easier.

Why is this two table into one table union, where showing invalid result in MySQL?

I have a table1 (records 3), and table2 (records 3).
Where i have field name in both.
Now i want to make a result from those two table
which will show me both table records and take only one if there is duplicate.
from that result i will do main query using like or or other logical statements
So my expected output records will contain 5 rows not 6 rows. How do i do that?
Example:
table1: table2:
+-------------------------+ +--------------------------------+
| Name | ID | Name | ID
+-------------------------- +---------------------------------
| A | 1 | 1 December Name | 4
| B | 2 | D | 5
| 1 December Name | 3 | E | 6
My Expected output is following which works, but does not work when i use WHERE
like to only get '1 December Name':
+-----------------------------------------------------+
| Name | ID
+-----------------------------------------------------
| A | 1 table1
| B | 2 table1
| 1 December Name | 3 table2 or table1 (no unique)
| D | 4 table2
| E | 5 table2
I tried this:
SELECT * FROM
(
(
SELECT name AS name FROM table1
)
UNION
(
SELECT anothername AS name FROM table2
)
) as t
WHERE name like '%1 December Name%'
limit 1,10
Output: Your SQL query has been executed successfully ( Query took 0.2798 sec )
Problem: The following query has no error but it does not find that record which contain '1 December Name'
Follow up: works i know now which ID it used
SELECT NAME, ID, STATUS FROM
(
(
SELECT NAME AS name , id, CONCAT('table1') AS STATUS FROM table1
)
UNION ALL
(
SELECT ANOTHERNAME AS name, id, CONCAT( 'table2' ) AS STATUS FROM table2
)
) AS t
WHERE
t.NAME LIKE '%1 December Name%'
LIMIT 1 , 10;
You can get something similar to what you want:
select name, group_concat(id) from
(select name, 'table1' as id from table1
union all
select name, 'table2' from table2) x
group by name
Output would be:
+------------------------------------+
| Name | ID
--------------------------------------
| A | table1
| B | table1
| 1 December Name | table1,table2
| D | table2
| E | table2
UNION ALL is the right choice (not UNION), because it does not remove duplicates, and preserves row order
Try this
(SELECT name FROM table1 )
UNION (SELECT name FROM table2);