Take two example tables:
TableOne
+----+----------+-------------+------------+------------+--+
| id | uid | thingone | thingtwo | thingthree | |
+----+----------+-------------+------------+------------+--+
| 1 | 7 | 0 | 1 | 1 | |
| 2 | 8 | 1 | 1 | 0 | |
+----+----------+-------------+------------+------------+--+
and
TableTwo
+----+----------+-------------+------------+------------+--+
| id | oid | thingone | thingtwo | thingthree | |
+----+----------+-------------+------------+------------+--+
| 1 | 7 | Apple | Coconut | Grape | |
| 2 | 8 | Potato | Orange | Banana | |
+----+----------+-------------+------------+------------+--+
So in this example userid 7 would get Coconut and Grape, but not Apple. Userid 8 would get Potato and Orange, but not banana. I am wanting to do this the most efficient way and I realize I could do two separate queries with some php processing, but I want to do it with a single query if feasible.
What I need to do is (in a single query) get the row from TableOne by id and then select the row from TableTwo where theoid = the id of TableOne but only get the corresponding row from TableTwo if it is 1 and not 0 in TableOne.
Can any one help please?
You can achieve it by using IF and comparing the columns from both tables 1 by 1. To compare the tabletwo from tableone you need to use JOIN, in this query I use a LEFT JOIN.
SELECT a.id, a.`uid`,
IF(a.`thingone`=1, b.`thingone`, NULL) AS thingone,
IF(a.`thingtwo`=1, b.`thingtwo`, NULL) AS thingtwo,
IF(a.`thingthree`=1, b.`thingthree`, NULL) AS thingthree FROM tableone a
LEFT JOIN tabletwo b ON uid=oid;
Check MySQL IF() Function for more details.
you need to make relational tables and then use join query
What about this:
select case when t1.thingone = 1 then t2.thingone else '' end,
case when t1.thingtwo = 1 then t2.thingtwo else '' end,
case when t1.thingthree = 1 then t2.thingthree else '' end
from TableOne t1 join TableTwo t2 on t1.uid=t2.oid
You might need to concatenate these three or convert them into three rows, depending how how the three things should be represented.
Related
Table 1
newpancard
id | name | cardno | status |
-----------------------------
1 | name1| 909099 | done |
2 | name2| 800099 | done |
3 | name3| 965099 | pending|
Table 2
oldpancard
id | name | cardno | status |
-----------------------------
1 | name4| 111119 | done |
2 | name5| 323239 | done |
3 | name6| 734349 | pending|
4 | name7| 609099 | done |
can we get fetch data from both tables where status = done in both tables?
I am trying the following query but getting duplicates data in bulk.
SELECT tb1.*, tb2.*
FROM `newpancard` tb1
JOIN `oldpancard` tb2
ON tb1.status = tb2.status
please correct me. Thanks
I think you actually want a UNION:
SELECT * FROM newpancard WHERE status='done'
UNION
SELECT * FROM oldpancard WHERE status='done'
We use a UNION (rather than UNION ALL) so we don't get duplicate records from newpancard and oldpancard
Output (from your sample data):
id name cardno status
1 name1 909099 done
2 name2 800099 done
1 name4 111119 done
2 name5 323239 done
4 name7 609099 done
SQLFiddle
I'm trying to concatenate data from three related tables according to:
orders orderrow orderrow_op
+----+ +----+----------+ +----+-------------+
| id | | id | id_order | | id | id_orderrow |
+----+ +----+----------+ +----+-------------+
| 1 | | 1 | 1 | | 1 | 1 |
| 2 | | 2 | 1 | | 2 | 1 |
| 3 | | 3 | 2 | | 3 | 2 |
+----+ | 4 | 3 | | 4 | 3 |
+----+----------+ | 5 | 3 |
| 6 | 3 |
+----+-------------+
The result i'm looking for is something like:
orderops (Desired Result)
+----------+-----------------+
| id_order | id_row:id_ops |
+----------+-----------------+
| 1 | 1:(1,2); 2:(3); |
| 2 | 3:(4,5,6) |
| 3 | 4:NULL |
+----------+-----------------+
I.e i want the operations and rows all be displayed on one row related to the order. So far i've tried things like:
SELECT
db.orders.id AS orderid,
db.orderrow.id AS rowids,
GROUP_CONCAT(DISTINCT db.orderrow.id) AS a,
GROUP_CONCAT(db.orderrow.id, ':', db.orderrow_op.id) AS b
FROM
db.orders
LEFT JOIN db.orderrow ON db.orders.id = db.orderrow.id_order
LEFT JOIN db.orderrow_op ON db.orderrow.id = db.orderrow_op.id_orderrow
GROUP BY orderid
Where in column 'a' i get the row ids and in column 'b' i get the operation_ids with corresponding row_id prepended. I'd like to combine the two into a single column such that related values in 'b' will start of with id from 'a' and only show once.
I'm fairly new to MySQL so i don't know if this is even possible or if i'ts a good idea at all? The aim is to structure the data into JSON for delivery via REST application so perhaps it's better to deliver the rows directly to the webserver and handle json parsing over there? I just figured that this approach might be faster.
This is not the nicest query but it's working for your example table setup.
SELECT
o.id AS id_order,
group_concat(sub.ops
SEPARATOR ' ') AS id_row_id_ops
FROM
(SELECT
orderrow.id_order,
IF(isnull(l3.ops), concat(orderrow.id, ':', 'NULL'), concat(orderrow.id, ':', l3.ops)) as ops
FROM
orderrow
LEFT JOIN (SELECT
orderrow_op.id_orderrow,
concat('(', group_concat(orderrow_op.id), '); ') as ops
FROM
orderrow_op
GROUP BY orderrow_op.id_orderrow) l3 ON l3.id_orderrow = orderrow.id) sub
LEFT JOIN
orders o ON o.id = sub.id_order
GROUP BY o.id;
One of the things to mind is the LEFT JOIN and that you need to cast a "null" value to a "null" text (otherwise your element 4 will vanish).
The output:
i have a question concerning the comparison of tuples in mysql.
I have two tables:
Person_ID | Class_ID | STATE
1 | 1 | 1
1 | 3 | 0
Person_ID | Class_ID
1 | 1
1 | 2
1 | 3
Thus, the second row is not existing in the first table. However, I want to combine the tables like:
Person_ID | Class_ID | STATE
1 | 1 | 1
1 | 2 | -1
1 | 3 | 0
Someone an idea, how I can do that?
I tried to play with exist, if, where, etc. :-(.
You can use a LEFT JOIN with COALESCE:
SELECT t2.Person_ID, t2.Class_ID,
COALESCE(t1.STATE,-1) AS STATE
FROM Table2 AS t2
LEFT JOIN Table1 AS t1
ON t2.Person_ID = t1.Person_ID AND t2.Class_ID = t1.Class_ID
I have some issues with multiple joins in MysQL
I have 3 tables:
cms_data_company
cms_data_company_categories
cms_datasrc_category
Sample records from: cms_data_company:
+----+-----------+------------+
| id | name | address |
+----+-----------+------------+
| 1 | Name1 | Samplestr1 |
+----+-----------+------------+
| 2 | Name2 | Samplestr2 |
+----+-----------+------------+
| 3 | Name3 | Samplestr3 |
+----+-----------+------------+
Sample records from: cms_data_company_categories ( It contains Company_id field and category_id ) Point is that there is serveral records for one company_id )
+----+-----------+------------+
| id | company_id| category_id|
+----+-----------+------------+
| 1 | 2 | 14 |
+----+-----------+------------+
| 2 | 2 | 11 |
+----+-----------+------------+
| 3 | 1 | 15 |
+----+-----------+------------+
Sample records from: cms_datasrc_category ( Here is a issue that i need only that rows where:
datasrc = 1 AND parent = 0
+----+-----------+------------+-----------+
| id | datasrc | parent |name |
+----+-----------+------------+-----------+
| 1 | 1 | 0 |category1 |
+----+-----------+------------+-----------+
| 2 | 2 | 0 |category2 |
+----+-----------+------------+-----------+
| 3 | 3 | 5 |category3 |
+----+-----------+------------+-----------+
What i would like to recive is that:
All fields from cms_data_company and field name from cms_datasrc_company
I need to join it as follows:
id from cms_data_company match with company_id from cms_data_company_categories
Then matched category_id from
cms_data_company_categories with ID from cms_datasrc_category (only these records where datasrc=0 and parent=0)
Return name as new column with all field from cms_data_company
I think I could make it messy, but my MySQL statement is as follows:
select * ,cms_datasrc_category.name_en
from cms_data_company
LEFT JOIN cms_data_company_categories.company_id
ON cms_data_company.id = cms_data_company_categories.company_id
LEFT JOIN cms_datasrc_category
ON cms_data_company_categories.category_id = cms_datasrc_category.id
WHERE cms_datasrc_category.datasrc = 1 AND cms_datasrc_category.parent = 0
It seems It is working somehow but, there is only records from cms_data_company where query can find something. I would like to change my statemat to show NULLs when There is no matching fields.
It is because WHERE applies to all Query ?
When you use left joins, conditions on all but the first table should be in the on clauses:
SELECT *, cdc.name_en
FROM cms_data_company dc LEFT JOIN
cms_data_company_categories.company_id c
ON dc.id = c.company_id LEFT JOIN
cms_datasrc_category cdc
ON c.category_id = cdc.id AND
cdc.datasrc = 1 AND cdc.parent = 0;
Notes:
Table aliases make a query easier to write and to read.
You should use table aliases for all column references, when your query has more than one table.
The select * already selects all columns from all tables. There is no need to include another column. Or, better yet, list the columns you really need.
The filtering conditions are on the last table, so they are now in the on clause.
I am sure this would be easy to google if I knew the right words to use, but I've tried and not come up with anything: apologies if this is a common question on SO.
I have one table which lists a set of records which can be one of 4 types.
table_1:
+-------+------------+------+
| id | value | type |
+-------+------------+------+
| 1 | x | 1 |
| 2 | y | 1 |
| 3 | z | 2 |
| 4 | a | 3 |
+-------+------------+------+
I have another table which references the id of this table and stores data
table_2:
+-------+------------+------+
| id | table_1_id |value |
+-------+------------+------+
| 1 | 4 | A |
| 2 | 2 | B |
| 3 | 3 | C |
| 4 | 2 | D |
+-------+------------+------+
I want to write a query that effects:
"Find all the records from table 1 which are of type 1, take the id's of those records, and find all the records in table 2 where 'table_1_id' which match one of that set of ids."
In the above very oversimplified table example that would result in the query returning records with ids 2 and 4 in table 2
Sounds like your looking for IN:
select *
from table2
where table_1_id in (select id from table1 where type = 1)
Or perhaps you could JOIN the tables:
select t2.*
from table2 t2
join table1 t1 on t2.table_1_id = t1.id
where t1.type = 1
Joining the tables could result in duplicate records. Depends on your needs.
SELECT t1.value,t1.type,t2.value FROM table1 t1,table2 t2 WHERE t1.id = t2.table_1_id AND t1.type = 1;